Hi wondered if you could see why my button element is rendered twice. The function is called inside image.on('load',function(){}). Here is a pen
and the code below. Thank you.
import paper from "https://esm.sh/paper";
window.onload = setup;
let buttonWrapper = null
function setup() {
paper.setup('canvas');
buttonWrapper = document.querySelector('.button-wrapper');
const image = new paper.Raster({
source: 'https://assets.codepen.io/1070/texture_planet_earth_color_color.jpg',
position: paper.view.center,
});
image.on('load', function () {
console.log("the original size of the image", image.width);
paper.project.view.viewSize = new paper.Size(image.width, image.height);
setImageSize(image, 220, paper.view.size);
paper.project.activeLayer.position = paper.view.center;
// Call download function after setImageSize
});
image.onError = function () {
console.log('image error');
};
}
function setImageSize(img, targetWidth, canvas) {
const canvasSize = Math.min(canvas.width, canvas.height);
const gridSize = canvasSize / 120;
const spacing = gridSize / 18;
const originalWidth = img.width;
const originalHeight = img.height;
img.crossOrigin = "";
const originalAspectRatio = originalWidth / originalHeight;
const newWidth = targetWidth;
const newHeight = targetWidth / originalAspectRatio;
img.size = new paper.Size(newWidth, newHeight);
img.position = paper.project.view.center;
// Clear existing spots
paper.project.activeLayer.removeChildren();
download(buttonWrapper);
for (let x = 0; x < img.width; x++) {
for (let y = 0; y < img.height; y++) {
let color = img.getPixel(x, y);
const spot = new paper.Path.Circle({
center: new paper.Point(x * gridSize, y * gridSize),
radius: gridSize / 2 / spacing,
fillColor: 'black',
});
spot.scale(1 - color.gray);
}
}
paper.project.activeLayer.position = paper.view.center;
}
function download(wrapper) {
console.log('function run')
const button = document.createElement('button');
button.textContent = "Download";
wrapper.append(button);
}