Replies: 1 comment
-
I am not exactly sure what you're asking. You're correct, to draw to the same texture without erasing what's there you use The canvas texture is always temporary. You call So, if you want what as drawn before to appear again you have 2 options
function render() {
updateBallPosition();
updatePaddlePositions();
const canvasTexture = context.getCurrentTexture()
drawNet(canvasTexture);
drawPaddles(canvasTexture);
drawBall(canvasTexture);
requestAnimationFrame(render);
}
requestAnimationFrame(render);
} Where you use If you want to do something like photoshop, where you draw into a texture and keep drawing on top of it, then you need to make your own texture. You draw to that texture, and then draw that texture to the canvas. Considering that texture you create to be like a "document" in photoshop. It can be any size yo want, and when you draw it you decide which portion to display and what size to display it. In that case it would be more like this const documentTexture = device.createTexture({...});
function onPointerMove(event) {
const pos = convertPointerCoordinateToDocumentCoordinate(event);
drawBrushToTexture(documentTexture);
// now draw texture to canvas
const canvasTexture = context.getCurrentTexture()
drawDocumentTexture(documentTexture, canvasTexture);
}
} Note: it would probably be best to instead of drawing the canvas in the mouse event itself, just queue a requestAnimationFrame (if you have not already queued one) to draw the texture to a canvas. That way, if you get lots of pointer events in the same frame, you don't waste time trying to update the canvas multiple times in a frame. example: https://jsgist.org/?src=75fea750283c11851e9e5f45e73c8134 also a version with multi-sampling |
Beta Was this translation helpful? Give feedback.
-
Even if it is slow.. I tried to actually get it working. From Vulkan I know that there are use cases where one want to render multiple times to the same texture. But even with more experimentation I didn't get it working. The shown example produces an error that the texture we are rendering to, has been already destroyed. And yes I found a stack overflow article about it that when the texture is submitted it gets destroyed. But within a frame the getCurrentTexture() method should return a new instance pointing to the same image.
Since I know Vulkan a bit tried to created a second renderPassDescriptor with loadOp = "load" so that the next submit would not overwrite the previous rendered triangle. Then I see that when I resize the window, a triangle at a previous location is sometimes visible... but the end result is that one triangle is drawn at the right side but nothing else...
Code from tutorial:
`// BAD! Slow!
for (let x = -1; x < 1; x += 0.1) {
uniformValues.set([x, 0], kOffsetOffset);
device.queue.writeBuffer(uniformBuffer, 0, uniformValues);
And here is my last test:
`async function Render()
{
const aspect = canvas.width / canvas.height;
uniformValues.set([0.5 / aspect, 0.5], scaleOffset);
Beta Was this translation helpful? Give feedback.
All reactions