let my={} function init(){let version='0.84' let s='' s+='
' s+='
' s+=wrap({tag:'btn',style:'width:90px; z-index:44;',fn:'optPop()'},'Restart') s+=wrap({tag:'btn',style:'width:90px; z-index:44;',fn:'moreBtn()'},'Jigsaws') s+=wrap({id:'imageBtn',tag:'btn',class:'hi',style:'width:90px; z-index:44;',fn:'toggleImg()'},'Image') s+=getPopHTML() s+='
' s+='
' s+='' s+='
' s+=wrap({cls:'copyrt',pos:'abs',style:'left:5px; bottom:3px'},`© 2021 MathsIsFun.com v${version}`) s+='
' docInsert(s) my.can=new Can('canvasId',1000,1000,1) my.jig=document.getElementById('jig') my.fileName='passiflora.html' let imgName=getQueryVariable('img') if(imgName){imgName=decodeURIComponent(imgName) imgName=imgName.replace(/[^0-9A-Za-z\-\.]+/g,'') my.fileName=imgName} console.log('fileName',my.fileName) my.imgHome=(document.domain=='localhost'?'/mathsisfun':'')+'/puzzles/images/jigsaw/' my.rowN=4 my.colN=4 my.pWd=100 my.pHt=100 my.animN=0 my.animTp=0 my.imgQ=true my.p=[] window.addEventListener('mousedown',mouseDown,false) my.touchSelPiece=null my.touchPt=null my.jig.addEventListener('touchstart',ontouchstart,false) my.jig.addEventListener('touchmove',ontouchmove,false) my.jig.addEventListener('touchend',ontouchend,false) optPop()} function toggleImg(){my.imgQ=!my.imgQ if(my.imgQ){loadImg(0.5,false)}else{loadImg(0,false)} toggleBtn('imageBtn',my.imgQ)} function toggleBtn(btn,onq){if(onq){document.getElementById(btn).classList.add('hi') document.getElementById(btn).classList.remove('lo')}else{document.getElementById(btn).classList.add('lo') document.getElementById(btn).classList.remove('hi')}} function ontouchstart(evt){evt.preventDefault() let touch=evt.targetTouches[0] startIt(touch.clientX,touch.clientY)} function ontouchmove(evt){let touch=evt.targetTouches[0] my.touchSelPiece.moveTo(touch.pageX-my.touchPt.x,touch.pageY-my.touchPt.y) my.touchPt=new Point(touch.pageX,touch.pageY) evt.preventDefault()} function ontouchend(evt){finishIt() evt.preventDefault()} function moreBtn(){window.location.href='http://www.mathsisfun.com/puzzles/jigsaw-puzzles-index.html'} function optPop(){let pop=document.getElementById('optpop') pop.style.transitionDuration='0.3s' pop.style.opacity=1 pop.style.zIndex=12 pop.style.left='120px' pop.style.top='20px'} function userUpdate(){} function getPopHTML(){let s='' s+='
' let opts=[] for(let i=3;i<=12;i++){opts.push({name:i,descr:i})} s+=wrap({id:'rowN',tag:'sel',lbl:'Rows:',txt:'6',opts:opts}) s+='   ' s+=wrap({id:'colN',tag:'sel',lbl:'Cols:',txt:'6',opts:opts}) s+='
' s+='' s+='
' s+='
' return s} function editYes(){let pop=document.getElementById('optpop') pop.style.opacity=0 pop.style.zIndex=1 pop.style.left='-500px' my.rowN=document.getElementById('rowN').value my.rowN=Math.max(3,Math.min(my.rowN>>0,12)) my.colN=document.getElementById('colN').value my.colN=Math.max(3,Math.min(my.colN>>0,12)) if(my.imgQ){loadImg(0.5,true)}else{loadImg(0,true)}} function doAnim(){let pc=my.p[my.animN] if(my.animTp%2==0){pc.left=pc.canvas.style.left let left=parseFloat(pc.canvas.style.left) pc.canvas.style.left=left-(Math.random()-0.5)*60+'px' pc.top=pc.canvas.style.top let top=parseFloat(pc.canvas.style.top) pc.canvas.style.top=top-(Math.random()-0.5)*60+'px'}else{pc.canvas.style.left=pc.left pc.canvas.style.top=pc.top} my.animN++ if(my.animN=document.body.clientWidth-rect.left-my.pWd*2){xCurr=-rect.left yCurr+=my.pHt*1.5}} return shuffle(pts)} function shuffle(array){let tmp,current,top=array.length if(top) while(--top){current=Math.floor(Math.random()*(top+1)) tmp=array[current] array[current]=array[top] array[top]=tmp} return array} function go(img){my.pWd=img.width/my.colN my.pHt=img.height/my.rowN console.log('go',img,my.pWd,my.pHt) let pts=getRandPts() let ptNo=0 let p=[] let count=0 for(let l=0;l0){this.left.points=this.makeSide(this.left)}else{this.left.points=[] this.left.points.push(this.left.first) this.left.points.push(this.left.last)} if(obj.top.first.y>0){this.top.points=this.makeSide(this.top)}else{this.top.points=[] this.top.points.push(this.top.first) this.top.points.push(this.top.last)} if(parseFloat(obj.right.first.x)>=parseFloat(obj.imgwidth)-1){this.right.points=[] this.right.points.push(this.right.first) this.right.points.push(this.right.last)}else{this.right.points=this.makeSide(this.right)} if(parseFloat(obj.bot.last.y)>=parseFloat(obj.imgheight)-1){this.bot.points=[] this.bot.points.push(this.bot.first) this.bot.points.push(this.bot.last)}else{this.bot.points=this.makeSide(this.bot)} this.display=function(){this.context.beginPath() this.dBezierCurve(this.top.points) this.dBezierCurve(this.right.points) this.dBezierCurve(this.bot.points) this.dBezierCurve(this.left.points) this.context.closePath() this.context.save() this.context.clip() this.context.drawImage(this.image,my.pWd/2-this.imgtop.first.x,my.pHt/2-this.imgtop.first.y) this.context.strokeStyle='#333' this.context.lineWidth=1 this.context.stroke()}} return PuzzlePiece})() PuzzlePiece.prototype={rotate:function(around){this.connected.forEach(function(element){let temptop=parseFloat(around.canvas.style.top)-(parseFloat(around.canvas.style.left)-parseFloat(element.canvas.style.left)) let templeft=parseFloat(around.canvas.style.left)+(parseFloat(around.canvas.style.top)-parseFloat(element.canvas.style.top)) element.canvas.style.setProperty('top',temptop+'px') element.canvas.style.setProperty('left',templeft+'px') element.canvas.getContext('2d').restore() element.canvas.getContext('2d').clearRect(0,0,element.canvas.width,element.canvas.height) element.context.translate(element.canvas.width/2.0,element.canvas.height/2.0) element.canvas.getContext('2d').rotate(Math.PI/2.0) element.rotation+=1 element.rotation%=4 element.context.translate(-(element.canvas.width/2.0),-(element.canvas.height/2.0)) element.display() let origtop=element.piecetop let origright=element.pieceright let origbot=element.piecebot let origleft=element.pieceleft element.piecetop=null element.pieceright=null element.piecebot=null element.pieceleft=null element.piecetop=origleft element.pieceright=origtop element.piecebot=origright element.pieceleft=origbot})},moveTo:function(x,y){this.connected.forEach(function(element){element.canvas.style.setProperty('left',parseFloat(element.canvas.style.left)+x+'px') element.canvas.style.setProperty('top',parseFloat(element.canvas.style.top)+y+'px')})},dBezierCurve:function(){let controlpoint=3.5 let args=Array.prototype.slice.call(arguments) if(args[0].length>1){args=args[0]} if(args.length<=2){this.context.lineTo(args[0].x,args[0].y) this.context.lineTo(args[1].x,args[1].y) return} let a=[] a.push(args[0]) for(let j=0;jtopmatchmin&&toptop+e.heightleftmatchmin&&toplefttopmatchmin&&righttopleftmatchmin&&rightleft-e.widthtopmatchmin&&lefttopleftmatchmin&&leftleft+e.widthtopmatchmin&&bottop-e.heightleftmatchmin&&botleft{if(cls.length==0)cls='btn' return '