xxxxxxxxxx
47
// This sketch calculates the canvas size needed to obtain a
// specific image size in millimeters at given DPI.
//
// Usually for print the canvas may by much bigger than
// the available preview size, so scale it as well.
// Pierre Rossel - 2024-04-16
// Size of the saved image and its DPI
const mmWidth = 895; // width in millimeters
const mmHeight = 1280; // height in millimeters
const dpi = 150;
function setup() {
// Calculate the canvas size
const wCanvas = round(((mmWidth / 25.4) * dpi) / pixelDensity());
const hCanvas = round(((mmHeight / 25.4) * dpi) / pixelDensity());
print(wCanvas, hCanvas);
// Create the canvas
createCanvas(wCanvas, hCanvas);
// Reduce size of preview
// Adjust as needed to fit the canvas in the preview area
let scalePreview = 0.2;
let cnv = document.getElementById("defaultCanvas0");
cnv.style.width = round(width * scalePreview) + "px";
cnv.style.height = round(height * scalePreview) + "px";
// Button to save image
createButton("Save image").mousePressed(() => save(
"Canvas_" + year() + nf(month(), 2) + nf(day(), 2) +
"_" + nf(hour(), 2) + nf(minute(), 2) + nf(second(), 2) + ".jpg"
));
}
function draw() {
background(220);
noStroke();
translate(width / 2, height / 2);
rotate(millis() / 500);
ellipse(width / 4, 0, width / 4);
}