const saveImage = () => {
const image = document.querySelector("img");
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
canvas.width = 300;
canvas.height = 200;
ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
ctx.lineWidth = 5;
ctx.strokeStyle = "red";
ctx.strokeRect(0, 0, canvas.width, canvas.height);
document.body.appendChild(canvas);
};
saveImage();
либо
const saveImage = () => {
const image = document.querySelector("img");
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
canvas.width = 300;
canvas.height = 200;
ctx.translate(canvas.width / 2, canvas.height / 2);
ctx.drawImage(
image,
-canvas.width / 2,
-canvas.height / 2,
canvas.width,
canvas.height
);
ctx.lineWidth = 5;
ctx.strokeStyle = "red";
ctx.strokeRect(
-canvas.width / 2,
-canvas.height / 2,
canvas.width,
canvas.height
);
document.body.appendChild(canvas);
};
saveImage();