Canvas - permanently apply Y-axis offset to all future drawings

I want to have everything drawn onto the canvas be given an offset in the Y axis which will increase each frame, imagine gravity (without acceleration, just constant speed) and there's no ground at the bottom of the canas to prevent objects from falling off. I want this to apply to everything, not just some drawings, not just stuff in the foreground, I want the entire canvas to shift down by a fixed number of pixels every animation frame. I thought I might be able to do this by using ctx.translate(0, 10) without wrapping it in save/restore, but this hasn't seemed to work. Here's a snippet from one of my draw functions in case anything here is causing issues;
canvas.ctx.save();
canvas.ctx.translate(this.position.x, this.position.y);
canvas.ctx.rotate((this.position.angle * Math.PI) / 180);
canvas.ctx.beginPath();
canvas.ctx.rect(-15, -25, 30, 50);
canvas.ctx.fillStyle = "#000000";
canvas.ctx.fill();
canvas.ctx.closePath();
canvas.ctx.restore();
canvas.ctx.save();
canvas.ctx.translate(this.position.x, this.position.y);
canvas.ctx.rotate((this.position.angle * Math.PI) / 180);
canvas.ctx.beginPath();
canvas.ctx.rect(-15, -25, 30, 50);
canvas.ctx.fillStyle = "#000000";
canvas.ctx.fill();
canvas.ctx.closePath();
canvas.ctx.restore();
canvas.ctx is a CanvasRenderingContext2D for my canvas. My animation frame loop has the following;
function animate() {
canvas.ctx.translate(0, 10); // this appears to do nothing, does `.translate()` reset itself each call?
thatDrawFunction();
requestAnimationFrame(animate);
}
function animate() {
canvas.ctx.translate(0, 10); // this appears to do nothing, does `.translate()` reset itself each call?
thatDrawFunction();
requestAnimationFrame(animate);
}
of course, this is minimum reproduction, my actual code spans way too much to put in this thread
5 Replies
MarkBoots
MarkBoots2y ago
Think something like this should work. Clear the canvas, save, translate by amount * frameIndex, draw the thing and restore. then call the function again with increased frameIndex
function animate(frameIndex = 0) {
canvas.ctx.clearRect(0, 0, canvas.width, canvas.height);
canvas.ctx.save();
canvas.ctx.translate(0, 10 * frameIndex);
thatDrawFunction();
canvas.ctx.restore();
requestAnimationFrame(()=> animate(++frameIndex));
}
function animate(frameIndex = 0) {
canvas.ctx.clearRect(0, 0, canvas.width, canvas.height);
canvas.ctx.save();
canvas.ctx.translate(0, 10 * frameIndex);
thatDrawFunction();
canvas.ctx.restore();
requestAnimationFrame(()=> animate(++frameIndex));
}
small example: (removed pen, see screenshot below)
WillsterJohnson
ss
MarkBoots
MarkBoots2y ago
What is ss?
MarkBoots
MarkBoots2y ago
pen removed, backup
WillsterJohnson
sorry, my laptop likes to focus the wrong window, I think I was playing a game? idk I ended up coming across something similar to that on SO, this seems to be cleaner though!
Want results from more Discord servers?
Add your server
More Posts
useSelector() in Redux not rendering new value ❓I am learning *redux* , so made a demo webApp and in that I am incrementing and decrementing the valHello I've been trying to figuring out how to make a link that lets people email you.Hello I've been trying to figuring out how to make a link that lets people email you. like that linkhow to paste something into an input from clipboard without using document.ExecCommand('paste') ?how to paste something into an input from clipboard without using document.ExecCommand('paste') sincneed a library that helps me to resize the window (we want the layout like codesandbox).hey! everyone... I'd like to ask you a question: I'm looking for a library that will allow me to resCSSComb for nowadays SASSi work for a long time now with a tool called CSSComb that allows me to order my CSS declarations inI want to extend builtin HTML element classes (I know why you shouldn't and cant)`class MyCanvas extends HTMLCanvasElement` is obviously out of the question, but there's surely someHow can I align some paragraphs to start at the centered title's starting pointI am working on this the whole day, still can't manage to do it. The problem is I have titles in my flowHi Gays, I found this code but I don't know what it does : ```css .flow > *:where(:not(:first-child)how do i get input values inputted into input field 'in real-time' and use the input values?here's code i've tried: ```tipButtons.forEach(tipButton => { tipButton.addEventListener('click'make input field expand verticallyhi, i want to make my input field expand vertically when the text overflows, i attached a quick vide