const posters = document.getElementsByClassName('poster'); for(let i=0;i imgLeft && peelSizeY > imgTop ) { peelSizeX -= putSpeedX; peelSizeY -= putSpeedY; }else{ peelSizeX = imgLeft; peelSizeY = imgTop; direction = false; } }else{ if (peelSizeX < maxPeelSizeX && peelSizeY < maxPeelSizeY ) { if(peelSizeX < maxPeelSizeX){ peelSizeX += peelSpeed; } if(peelSizeY < maxPeelSizeY){ peelSizeY += peelSpeed; } }else{ repeat = false; } } ctx.clearRect(0, 0, canvasWidth, canvasHeight); // キャンバスをクリア // クリッピングのための多角形を描画 ctx.save(); ctx.beginPath(); ctx.moveTo(imgLeft, peelSizeY); ctx.lineTo(peelSizeX, imgTop); ctx.lineTo(imgRight, imgTop); ctx.lineTo(imgRight, imgBottom); ctx.lineTo(imgLeft, imgBottom); ctx.closePath(); // クリッピングを適用 ctx.clip(); ctx.drawImage(img, imgLeft, imgTop, imgWidth, imgHeight); ctx.restore(); // 剥がれた部分の裏側を描画 drawPeelingPart(peelSizeX, peelSizeY, gravity); if(repeat){ requestAnimationFrame(animate); } } function drawPeelingPart(sizeX, sizeY, gravity) { const foldHeight = sizeY + gravity * 10; // 重力を加味して垂れ下がるようにする // 剥がれた部分の裏側を描画 ctx.save(); ctx.beginPath(); ctx.moveTo(sizeX, imgTop); ctx.lineTo(sizeX - sizeX * 0.3, foldHeight); // 垂れ下がるように描画 ctx.quadraticCurveTo(sizeX - sizeX * 0.4, foldHeight * 0.9, imgLeft, sizeY); ctx.closePath(); // 裏側の色 ctx.fillStyle = '#dedede'; ctx.fill(); // 影の効果 ctx.shadowColor = 'rgba(0, 0, 0, 0.5)'; ctx.shadowBlur = 10; ctx.shadowOffsetX = -5; ctx.shadowOffsetY = 5; ctx.fill(); ctx.restore(); } animate(); }