1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| var canvas = document.querySelector('canvas'), oBtn = document.querySelectorAll('button'), ctx = canvas.getContext('2d'); var _x = 0, _y = 0, x = 0, y = 0;
canvas.width = 300; canvas.height = 200;
canvas.addEventListener('touchstart', function (e) { e.preventDefault(); _x = e.touches[0].pageX, _y = e.touches[0].pageY; ctx.beginPath(); ctx.moveTo(_x - canvas.offsetLeft, _y - canvas.offsetTop);
this.addEventListener('touchmove', function (e) { x = e.touches[0].pageX, y = e.touches[0].pageY; ctx.lineTo(x - canvas.offsetLeft, y - canvas.offsetTop); ctx.stroke(); }); });
oBtn[0].onclick = function () { var oImg = new Image(); ctx.drawImage(oImg, 0, 0); oImg.src = canvas.toDataURL('image/png'); document.body.appendChild(oImg); }
oBtn[1].onclick = function () { ctx.clearRect(0, 0, canvas.width, canvas.height); }
|