I want to resize a canvas using canvas.size(w, h)
but it keeps freezing my code!!!
What I’ve tried
- Converting the canvas (I think is
p5.Renderer
) into an element by doing this:new p5.Element(canvas.elt, p5)
, but it still freezes it - Using
resizeCanvas()
, did not resize the canvas, but at least it did not freeze my code - Setting the
isMainCanvas
propriety in thep5.Renderer
constructor totrue
My Code:
The one i am using is resizeCanvasByPercent()
.
/**
* @param {HTMLCanvasElement || p5.Renderer || p5.Element} canvas - The canvas you want to resize
* @param {Number} size - The new width or both width and height of the canvas you want to change
* @param {Number} [h=] - The new height of the canvas
* @param {Boolean} [useAsNewRenderer=] - If you want to use the new canvas as the default one
*
* @returns { {width: Number, height: Number} }
*/
function resizeCanvasPx(canvas, size, h = size, useAsNewRenderer = false) {
if (canvas instanceof HTMLCanvasElement) { canvas = new p5.Element(canvas, p5)}
if (canvas instanceof p5.Renderer) { canvas = new p5.Element(canvas.elt, p5) }
console.log(canvas.size)
canvas.size(size, h)
console.log(canvas)
return new p5.Element(canvas.elt, p5).size()
}
/**
* @param {HTMLCanvasElement || p5.Element || p5.Renderer} canvas - the canvas you want to resize
* @param {Number} percent - The new width or both width and height percents you want to change
* @param {Number} [heightPercent=] - The new height percent
*
* @returns { {width: Number, height: Number} }
*/
function resizeCanvasByPercent(canvas, percent, heightPercent = percent) {
if (canvas instanceof HTMLCanvasElement) { canvas = new p5.Element(canvas) }
if (canvas instanceof p5.Renderer) { canvas = new p5.Element(canvas.elt, p5) }
const canvasParentSize = new p5.Element(canvas.parent(), p5).size();
const w = canvasParentSize.width * (percent / 100)
const h = canvasParentSize.height * (heightPercent / 100)
console.log(resizeCanvasPx(canvas, w, h))
//console.log(resizeCanvasPx(canvas, 100, 100))
return resizeCanvasPx(canvas, w, h);
}
All my code: https://replit.com/@ErrorbotTHE2nd/Roblox-External-Editor#script.js