鋸齒狀這裏是我的代碼:HTML5畫布繪製成爲在平板
var can = $('#signature')[0],
ctx = can.getContext('2d'),
mousePressed = false,
mouseX = 0,
mouseY = 0;
can.addEventListener('touchmove', onTouchMove, false);
can.addEventListener('touchstart', onTouchStart, false);
can.addEventListener('touchend', onMouseUp, false);
// Tablet
function onTouchMove(event){
if (mousePressed) {
event.preventDefault();
mouseX = (event.targetTouches[0].pageX) - can.offsetLeft;
mouseY = (event.targetTouches[0].pageY) - can.offsetTop;
ctx.lineTo(mouseX, mouseY);
ctx.stroke();
}
}
function onTouchStart(event){
mousePressed = true;
mouseX = (event.targetTouches[0].pageX) - can.offsetLeft;
mouseY = (event.targetTouches[0].pageY) - can.offsetTop;
ctx.beginPath();
ctx.moveTo(mouesX, mouseY);
}
function onMouseUp(event){
mousePressed = false;
}
// Desktop
can.addEventListener('mousemove', onMouseMove, false);
can.addEventListener('mousedown', onMouseDown, false);
can.addEventListener('mouseup', onMouseUp, false);
function onMouseMove(event) {
if (mousePressed) {
event.preventDefault();
mouseX = event.clientX - can.offsetLeft;
mouseY = event.clientY - can.offsetTop;
ctx.lineTo(mouseX, mouseY);
ctx.stroke();
}
}
function onMouseDown(event) {
mousePressed = true;
mouseX = event.clientX - can.offsetLeft;
mouseY = event.clientY - can.offsetTop;
ctx.beginPath();
ctx.moveTo(mouseX, mouseY);
}
$('#clearsig')[0].addEventListener('click', clearSignature, false);
function clearSignature() {
ctx.clearRect(0, 0, can.width, can.height);
}
的HTML:
<canvas id="signature" width="800" height="350" style="border:1px solid #000;"></canvas>
在桌面上繪製的一切用鼠標罰款,我沒有問題。然而,在我的星系標籤3上當我在畫布上畫線時,線條畫出了一個曲折形狀。不知道爲什麼會發生這種情況或如何解決這個問題。
http://jsfiddle.net/KUaEF/在平板電腦上測試它,如果它工作正常,頁面上的其他內容會干擾畫布。小提琴在我的iOS設備上完美運行 – orhanhenrik
您是否在使用canvas元素(除了內聯)使用了一些CSS樣式? – K3N
只是樣式元素中的邊框@ Ken-AbdiasSoftware – kqlambert