DEV Community

Cover image for To Jigsaw or not to Jigsaw?
Dr Abstract
Dr Abstract

Posted on

To Jigsaw or not to Jigsaw?

I have been making interactive media since 1995... I have always avoided making jigsaw puzzles as they are the classic example and quite overdone. I would encourage my students to go beyond and at least make the pieces moving video parts or something different.

Well, on Discord, someone was posting problems using code with a blog post from a while back - - I started to help but then felt it would be better to update the code as the blog post was quite popular. The result is found on CodePen at - come give it a fork and heart!

It is fairly tricky to do a jigsaw puzzle. We have an alternative in ZIM called the Scrambler() which is just a few lines of code to implement. See But in doing the Jigsaw, we really had to use many parts of interactive media.

Alt Text

The puzzle parts themselves are drawn with a Shape. We figured it would be good use a class here and make it so we can pass the format of the piece as an array of four sides with 1 being a bump out, 0 being no bump and -1 being a bump in:

// ~~~~~~~~~~~~~~~~~~~~~
// CLASS to make JigSaw piece with bumps out or in or no bump on any four edges
class Piece extends Shape {
    // format is 1 for bump out, -1 for bump in and 0 for no bump
    constructor(w=100,h=100,format=[1,1,1,1],s=black,ss=4,f=white) {
        const p = Piece.part; // static property - defined below class
        const g =;
        // s() stroke, ss() strokeStyle, f() fill
        // mt() moveTo, lt() lineTo, ct() curveTo()
        if (format[0]==0),0); // top left-right
        else {
            let s = format[0]==1?-1:1; // sign                
            this.ct(w*(p-g/2), s*w*g, w/2, s*w*g); // curve left to middle
            this.ct(w*(p+g+g/2), s*w*g, w*(1-p), 0); // curve middle to right           
        if (format[1]==0),h); // right top-bottom
        else {
            let s = format[1]==1?1:-1; 
            this.ct(w+s*w*g, h*(p-g/2), w+s*w*g, h/2);
            this.ct(w+s*w*g, h*(p+g+g/2), w, h*(1-p));                
        if (format[2]==0),h); // bottom right-left
        else {
            let s = format[2]==1?1:-1;           
            this.ct(w*(p+g+g/2), h+s*w*g, w/2, h+s*w*g);
            this.ct(w*(p-g/2), h+s*w*g, w*p, h+0);                
        if (format[3]==0),0); // left bottom-top
        else {
            let s = format[3]==1?-1:1;             
            this.ct(s*w*g, h*(p+g+g/2), s*w*g, h/2);
            this.ct(s*w*g, h*(p-g/2), 0, h*p);             
        this.cp(); // close path
Piece.part = .37; // part of the edge with no gap ratio = 1-Piece.part*2; // gap ratio of edge
Enter fullscreen mode Exit fullscreen mode

As you can see, it is tricky to do the four sides using absolute positioning. Would have perhaps been better to use ZIM Generator() which does relative position - so we could use the same code for each side and rotate 90 before going to the next side. Anyway - it was not that bad, just a bit of mental twisting.

Alt Text

We wanted to have a version of the puzzle without the images that we could use as a hint. So the puzzle piece class did not use images - but rather just the shape.

Placing the shapes in position was also a challenge. We do not want bumps on the edges and the bumps have to align going from one piece to the next. We put a system in place inside the loop to handle the opposite of the last bump. Then we made sure there were no bumps for the edges.

// ~~~~~~~~~~~~~~~~~~~~~
// makePieces gets called from Tile - for each piece
let count=0;
let lastX = rand()>.5?1:-1; // 1 or -1 for out or in horizontally
let lastYs = []; // 1 or -1 vertically - remember with array and modulus
loop(numX, i=>{lastYs.push(rand()>.5?1:-1);});
function makePiece() {   

    // prepare format for jigsaw piece [1,0,-1,0] 
    // 1 bump out, 0 no bump, -1 bump in, etc.
    let currentX = lastX*-1; // opposite of last x
    let currentY = lastYs[count%numX]*-1; // opposite of last y
    let nextX = rand()>.5?1:-1; // randomize the next 1 or -1 for out or in horizontally
    let nextY = rand()>.5?1:-1; // and vertically
    // top, right, bottom, left
    let format = [currentY, nextX, nextY, currentX]; 
    lastX = nextX;
    lastYs[count%numX] = nextY;       

    // override edges to 0
    if (count < numX) format[0] = 0;
    else if (count >= numX*numY-numX) format[2] = 0;
    if (count%numX==0) format[3] = 0;
    else if ((count-numX+1)%numX==0) format[1] = 0;

    // make a container to hold jigsaw shape and later picture part
    let piece = new Container(w,h).centerReg({add:false});
    piece.puzzle = new Piece(w, h, format).addTo(piece);      
    piece.mouseChildren = false;  
    return piece;

const pieces = new Tile({
    clone:false // otherwise makes clone of piece
Enter fullscreen mode Exit fullscreen mode

ZIM has dynamic parameters (called ZIM VEE values - with Pick() That means we can pass a function into the obj parameter of the Tile and each time it goes to make an item it will take the return value of the function. This is one of the formats of a dynamic parameter in ZIM. You can also pass in an array which it will randomly pick from or a series that it pics from in order, or a {min, max} object or a combination of these. Very powerful!

For figuring if the puzzle piece is in the right place we made little hit boxes and added them to the hint version of the puzzle which always stays in the right place. We could have used hitTestGrid() to calculate the right place but the hit boxes is easier to visualize and work with.

// ~~~~~~~~~~~~~~~~~~~~~
const hint = pieces.clone(true) // exact
    .ord(-1) // under pieces     
    .cache(-5,-5,pic.width+10,pic.height+10) // cache by default does not include outside border 
    .vis(0); // checkbox below to show

// make a little box to do hit test to see if in right place
const snap = 50; // pixel distance considered correct
loop(hint, h=>{ = new Rectangle(snap,snap).centerReg(h).vis(0); // do not use alpha=0 as that will make it not hittable        
Enter fullscreen mode Exit fullscreen mode

We then want to scramble the pieces both in position and rotation and add events to test if the piece is in the right place and if the whole puzzle is done. This is tricky as a single tap should rotate the piece but this should not happen if the piece is dragged. ZIM has tap() built in to handle a quick tap at one location. We are also animating rotation and do not want that process interrupted as that might leave us with a partially rotated piece.

// ~~~~~~~~~~~~~~~~~~~~~
const padding = 50;
const rotate = true;
loop(pieces, (piece,i)=>{
    piece.alp(0); // sequence animation above will animate in alpha
    // test on mobile and see if you need to cache...
    // usually this is just cache() but the bumps are outside the piece 
    // and the cache size really does not make a difference if rest is background transparent 
    if (mob) piece.cache(-100,-100,piece.width+200,piece.width+200);
    if (rotate) {
        piece.rotation = shuffle([0,90,180,270])[0];
            time:.5, // within .5 seconds
            call:() => {   
                pieces.noMouse(); // do not let anything happen while animating until done
                    props:{rotation:String(frame.shiftKey?-90:90)}, // string makes relative
                    call:() => {
            call2:() => { // if no tap
    } else {
        piece.on("pressup", () => {
    piece.on("pressdown", () => {
        // shadows are expensive on mobile
        // could add it to container so shadow inside container 
        // then cache the container but might not be worth it
        if (!mob) piece.sha("rgba(0,0,0,.4)",5,5,5);

    // scramble location     
    piece.loc(padding+w/2+rand(stageW-w-padding*2)-pieces.x, padding+h/2+rand(stageH-h-padding*2)-pieces.y);        
Enter fullscreen mode Exit fullscreen mode

There are several places where we want to test the piece so we have put this functionality in a... function! We also quickly made an emitter for a reward here is the emitter code, followed by the test.

// EMITTER    
const emitter = new Emitter({
    obj:new Poly({min:40, max:70}, [5,6], .5, [orange, blue, green]),
Enter fullscreen mode Exit fullscreen mode
function test(piece) {
    let box = hint.items[piece.tileNum].box;
    if (piece.rotation%360==0 && box.hitTestReg(piece)) {
        if (placed==num) {
            stats.text = `Congratulations all ${num} pieces placed!`;
            timeout(1, function () {
                emitter.emitterForce = 8;
            timeout(2, function () {
                picCheck.checked = true;                    
                pieces.animate({alpha:0}, .7);
                outline.animate({alpha:0}, .7);
                hint.animate({alpha:0}, .7);
                pic.alp(0).animate({alpha:1}, .7);   
                new Button({
        } else stats.text = `Placed ${placed} piece${placed==1?"":"s"} of ${num}`;
    } else stage.update();    
Enter fullscreen mode Exit fullscreen mode

To see the full code please fork and heart the CodePen page at

All the best,

Dr Abstract - finally, a maker of a Jigsaw Puzzle.

Top comments (0)