1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| const setBannerImage = (h = "center", v = "middle") => { var canvas = document.getElementById("my-canvas"); var ctx = canvas.getContext("2d"); const canvasw = document.getElementById("banner_width").value; const canvash = document.getElementById("banner_height").value; const sourceImg = document.getElementById("my-image");
canvas.width = canvasw; canvas.height = canvash; const hvswitch = (value, wh) => ({ left: 0, center: (wh - canvas.width) / 2, right: wh - canvas.width, top: 0, middle: (wh - canvas.height) / 2, bottom: wh - canvas.height, }[value]);
sourceImg.onload = () => { if (h === "full") { ctx.drawImage(sourceImg, 0, 0, canvas.width, canvas.height); } else { ctx.drawImage( sourceImg, hvswitch(h, sourceImg.width), hvswitch(v, sourceImg.height), canvas.width, canvas.height, 0, 0, canvas.width, canvas.height ); } var imgbase64 = canvas.toDataURL(); banner.style.backgroundImage = `url('${imgbase64}')`; };
if (sourceImg.src && sourceImg) { if (h === "full") { ctx.drawImage(sourceImg, 0, 0, canvas.width, canvas.height); } else { ctx.drawImage( sourceImg, hvswitch(h, sourceImg.width), hvswitch(v, sourceImg.height), canvas.width, canvas.height, 0, 0, canvas.width, canvas.height ); } var imgbase64 = canvas.toDataURL(); banner.style.backgroundImage = `url('${imgbase64}')`; } };
|