Replicating Piet Mondrian’s art with code is no easy task, in fact honestly I’d say there’s no real way of pinning down his creations entirely with code, they were hand made. That said, we can try to replicate something within the realm of what would have been one of Piet’s works… so that’s what we will do in this tutorial… and yes, we will add the color as well.
As usual, here is our initial setup. Using window.devicePixelRatio
to scale the canvas on retina screens, and setting our canvas size, with only a html <canvas>
on the page.
|
|
Now, the approach I’d like to take isn’t perfect, but what I’d like to do is start with a big square (the canvas) and then start to divide it up… we will pick a line, either horizontally or vertically and break any squares in that area… after that, we’ll add some random to the splitting, so not all squares are split, which should give us something around the famous Mondrian look, albeit probably a little more mathematically rigid.
We’ll create an array of squares.
|
|
And, as I like to do, we’ll create our “draw” function and call it, so we can see what we’re making.
|
|
This is looping through all of our squares (just one at the moment, and drawing it on the canvas).
Now, we can create a function to find which squares should be split… and then the function to split the square in the direction we’ve given.
|
|
You can see at the bottom that I’m also calling the split the squares on the x
and y
, both in the middle. If these work, we’ll know we can do a lot more splitting. But for now, these will be great for testing.
First, the splitSquaresWith
function.
|
|
There’s a bit going on here, including some cheeky little tricks.
const { x, y } = coordinates
is extracting the x
and y
variables out of the object we’re passing, eg {x: 160}
or {y: 160}
(var i = squares.length - 1; i >= 0; i--)
this is because we’re taking elements out of the loop (and replacing them with 2 squares), looping backwards means the order will stay the same, and the new items won’t be split again.Of course, our single square has disappeared because we need to fill out the splitOn
functions, these are going to look pretty similar.
|
|
And…
|
|
These two functions are creating two squares, where the previous single square was, and then adding them back into our squares
array. You can see by splitting in the two centers, we’ve made a window.
Instead of our two practice splits, we’ll create a step
variable, and split on that step over and over.
|
|
And then the loop.
|
|
Whew, that was a lot of set up… we can get into the random
now. Rather than splitting each and every square when we hit it, we’ll only split them half the time.
|
|
Ooh, looking good. And on the y
axis too!
|
|
And there we have it, the shapes and structure we want! As always, with these tutorials you can hit the small arrow sitting between the editor and the demo and the code will rerun, if you press it a few times now, you’ll see our Mondrian structure take a few different shapes.
Let’s add some color in. First the variables, using those beautiful red, blue and yellow colors.
|
|
We’ll pick three random squares, and give each of them a color
. You might see only one or two colors, and that is because the same square was randomly selected twice.
|
|
And of course, making sure the draw function colors them in.
|
|
Colors, beautiful! Again, if you don’t see any, it should be a matter of hitting the arrow on the side a few times.
You can see now as well, how simple it is to add or remove complexity based on the grid.
|
|
|
|
|
|
And there we have it, a Mondrian. Please, hit the link below to have a play on CodePen, play with the colors, how you apply them, play with the split percentages. It’s all good fun!