Comments (2)
Hey @TheMapSmith! Thanks for trying out canvas-sketch.
There are two things in your sketch causing some issues right now:
- Your
triangle
function is adding lines to a path, but you never callcontext.beginPath()
– this means you are always adding commands to one big shape object, even across renders. - Your render function is not a "pure" render function — each time it gets called, its resulting in a different visual output (because of the random values). So what you initially see in the browser won't look the same as the export.
To fix issue 1, you need to be careful to use context.beginPath()
when you want to create a new shape:
function triangle(startX, startY, x1, y1, x2, y2) {
context.fillStyle = 'green'
// draw a triangle
context.beginPath();
context.moveTo(startX, startY);
context.lineTo(x1, y1)
context.lineTo(x2, y2)
context.closePath()
context.fill()
}
There are a number of ways to fix issue 2, but first you must understand why its being rendered multiple times:
When you hit Cmd + S
, canvas-sketch will re-render your canvas and call your render function again. This is because some props may have changed (like the { exporting }
prop), and depending on your settings the final export resolution may not match the browser resolution.
One easy solution is to use a seeded random – this means that the random numbers come in a predictable sequence that is the same across renders. It looks like this:
const sketch = () => {
// First let's generate a totally random seed string
const seed = random.getRandomSeed();
return ({ context, width, height }) => {
// Now, each time we render, reset the randomness to the above seed
random.setSeed(seed);
... the rest of your rendering code ...
};
};
Another solution is to separate the randomness/logic from your rendering code. Here is one example where the "geometry" is separated from the rendering. Since the data structure doesn't change between renders, it will appear the same even after you hit Cmd + S
multiple times.
Yet another solution is to forgo a render function entirely (see here for an example), and move all of your code to the sketch setup. This means that it will only be rendered once into the canvas. This is a bit less stable – if something changes the width
/height
of the canvas, you may lose the contents, or if you are using certain features of canvas-sketch like units, higher resolution exporting etc then it may not resize correctly.
Hope that clears things up a bit! And nice sketch. 😄
from canvas-sketch.
Thanks for the detailed reply with examples, Matt. I realized after my edit last night as I was lying in bed that I also changed the context.beginPath()
portion of my triangle()
function, which probably accounted for the fix. Thanks again for all you do!
from canvas-sketch.
Related Issues (20)
- Error with threejs OrbitControls and canvas-sketch --template=three
- Using a sketch on a website HOT 6
- Issue creating file sketch-01.js HOT 4
- Link to the three example in default template doesn't resolve HOT 4
- Error to install on macOS Monterrey
- issue opening sketch-01.js HOT 3
- Add canvas-sketch as canvas to website HOT 1
- Build files with CSS HOT 1
- Can't install FFMPEG Streaming HOT 1
- Placing two canvas elements on top of each other HOT 2
- Method for saving canvas HOT 4
- how to implement navigator.requestMIDIAccess(); HOT 2
- canvas-sketch mysketch.js HOT 3
- Live reloading not working properly
- Exporting multiple png HOT 1
- Problem installing HOT 2
- Duplicate canvases HOT 1
- Error: spawn EINVAL HOT 2
- how to integrate into svelte HOT 2
- issue in installing canvas-sketch HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from canvas-sketch.