Sto provando a scrivere una semplice applicazione di disegno basata su HTML (codice semplificato standalone allegato qui sotto). Ho provato questo sui seguenti dispositivi:Eventi di tocco lento lento su Android
- iPad 1 e 2: Grandi opere
- ASUS T101 con Windows: grandi opere
- Samsung Galaxy Tab: estremamente lento e irregolare - inutilizzabile.
- Lenovo IdeaPad K1: Estremamente lento e irregolare - inutilizzabile.
- Asus Transformer Prime: lag notevole rispetto all'iPad - vicino a utilizzabile.
Il tablet Asus esegue ICS, gli altri tablet Android sono in esecuzione 3.1 e 3.2. Ho provato usando il browser Android di serie. Ho anche provato l'Android Chrome Beta, ma era anche peggio.
Ecco un video che dimostra il problema: http://www.youtube.com/watch?v=Wlh94FBNVEQ
Le mie domande è il motivo per cui sono le tavolette Android così lento? Sto facendo qualcosa di sbagliato o è un problema ereditario con sistema operativo Android o browser, o c'è qualcosa che posso fare al riguardo nel mio codice?
multi.html:
<html>
<body>
<style media="screen">
canvas { border: 1px solid #CCC; }
</style>
<canvas style="" id="draw" height="450" width="922"></canvas>
<script class="jsbin" src="jquery.js"></script>
<script src="multi.js"></script>
</body>
</html>
multi.js:
var CanvasDrawr = function(options) {
// grab canvas element
var canvas = document.getElementById(options.id),
ctxt = canvas.getContext("2d");
canvas.style.width = '100%'
canvas.width = canvas.offsetWidth;
canvas.style.width = '';
// set props from options, but the defaults are for the cool kids
ctxt.lineWidth = options.size || Math.ceil(Math.random() * 35);
ctxt.lineCap = options.lineCap || "round";
ctxt.pX = undefined;
ctxt.pY = undefined;
var lines = [,,];
var offset = $(canvas).offset();
var eventCount = 0;
var self = {
// Bind click events
init: function() {
// Set pX and pY from first click
canvas.addEventListener('touchstart', self.preDraw, false);
canvas.addEventListener('touchmove', self.draw, false);
},
preDraw: function(event) {
$.each(event.touches, function(i, touch) {
var id = touch.identifier;
lines[id] = { x : this.pageX - offset.left,
y : this.pageY - offset.top,
color : 'black'
};
});
event.preventDefault();
},
draw: function(event) {
var e = event, hmm = {};
eventCount += 1;
$.each(event.touches, function(i, touch) {
var id = touch.identifier,
moveX = this.pageX - offset.left - lines[id].x,
moveY = this.pageY - offset.top - lines[id].y;
var ret = self.move(id, moveX, moveY);
lines[id].x = ret.x;
lines[id].y = ret.y;
});
event.preventDefault();
},
move: function(i, changeX, changeY) {
ctxt.strokeStyle = lines[i].color;
ctxt.beginPath();
ctxt.moveTo(lines[i].x, lines[i].y);
ctxt.lineTo(lines[i].x + changeX, lines[i].y + changeY);
ctxt.stroke();
ctxt.closePath();
return { x: lines[i].x + changeX, y: lines[i].y + changeY };
},
};
return self.init();
};
$(function(){
var drawr = new CanvasDrawr({ id: "draw", size: 5 });
});
Cosa c'è che non va con $ .each() di jQuery per enumerare una raccolta? – simbolo
Se stai facendo qualcosa che accade raramente, come dopo un domReady o pageResize, allora non c'è niente di sbagliato con $ .each(). Tuttavia, se stai facendo un'animazione, $ .each() è troppo lento. È un recinto, quindi deve creare un oggetto, fare un po 'di sporco, quindi rimuovere l'oggetto. Zakas fornisce una buona spiegazione nella sua presentazione ai programmatori di Google (vedi sopra). – mrbinky3000
Grazie, da allora ho cercato alcuni risultati di jsPerf (http://jsperf.com/jquery-each-vs-regular-for-vs-optimized-for-vs-foreach/3), ad esempio $ .each sembra essere molto più lento rispetto all'utilizzo di metodi JavaScript nativi. – simbolo