respuesta Editado
Con su aclaración, creo que puedo responder a esta apropiadamente ahora.
Para que el punto final siga la ruta que tomó el punto de inicio a través del lienzo, debe almacenar los valores históricos. En este ejemplo, http://jsfiddle.net/mobidevelop/bGgHQ/, utilizo los movimientos del mouse para llenar un buffer de las últimas 16 posiciones, luego lo uso para formar una curva bezier a través de los puntos al iterar sobre el RingBuffer.
function RingBuffer(length) {
this.length = length;
this.pointer = 0;
this.buffer = [];
}
RingBuffer.prototype.get = function(index) {
if (index < 0) {
index += this.length;
}
return this.buffer[index];
}
RingBuffer.prototype.push = function(value) {
this.buffer[this.pointer] = value;
this.pointer = (this.length + this.pointer +1) % this.length;
}
var c = document.getElementById("myCanvas");
var context =c.getContext("2d");
timer = window.setInterval(draw_line, 30);
function Point(x,y) {
this.x = x;
this.y = y;
}
Point.prototype.translateX = function(x) {
return this.x += x;
};
Point.prototype.translateY = function(y) {
return this.y += y;
};
function draw_line()
{
context.fillStyle = "#000";
context.fillRect(0, 0, c.width, c.height);
var pointer = history.pointer;
context.beginPath();
context.lineWidth = 2;
context.strokeStyle = '#F00';
for (iteration = 0, count = 15; iteration < count; iteration += 3) {
var p1 = history.get(--pointer);
var p2 = history.get(--pointer);
var p3 = history.get(--pointer);
var p4 = history.get(--pointer);
if (p1 && p2 && p3 && p4) {
context.moveTo(p1.x, p1.y);
context.bezierCurveTo(p2.x, p2.y, p3.x, p3.y, p4.x, p4.y);
}
pointer++;
}
context.stroke();
context.closePath();
}
var history = new RingBuffer(16);
var lastGrab = new Date();
c.addEventListener('mousemove', function() {
now = new Date();
if (now - lastGrab > 15) {
history.push(new Point(event.clientX - c.offsetLeft, event.clientY - c.offsetTop));
lastGrab = now;
}
});
respuesta anterior dejó a continuación, con fines históricos.
No estoy seguro de entender completamente lo que está tratando de lograr, pero creo que todo lo que necesita hacer es traducir todos sus puntos por el mismo valor. Esto dará como resultado que la línea atraviese el lienzo manteniendo la misma forma. algo como esto:
JSFiddle
var c = document.getElementById("myCanvas");
var context =c.getContext("2d");
timer = window.setInterval(draw_line, 30);
function Point(x,y) {
this.x = x;
this.y = y;
}
Point.prototype.translateX = function(x) {
return this.x += x;
};
Point.prototype.translateY = function(y) {
return this.y += y;
};
var p1 = new Point(0,0);
var p2 = new Point(100,100);
var cp1 = new Point(15,45);
var cp2 = new Point(85,45);
function draw_line()
{
context.fillStyle = "#000";
context.fillRect(0, 0, c.width, c.height);
context.beginPath();
context.lineWidth = 2;
context.strokeStyle = '#fff';
//Where p1, p2, cp1, cp2 are point objects that has x & y values already defined
context.moveTo(p1.x, p1.y);
context.bezierCurveTo(cp1.x,cp1.y,cp2.x,cp2.y,p2.x,p2.y);
context.stroke();
context.closePath();
p1.translateX(1);
p2.translateX(1);
cp1.translateX(1);
cp2.translateX(1);
if (p1.x > 300) {
p1.translateX(-400);
p2.translateX(-400);
cp1.translateX(-400);
cp2.translateX(-400);
}
}
A menos que yo estoy mal entendido el objetivo ...
Gracias por su respuesta, pero lo que estoy tratando de lograr es un punto que tiene una cola. el primer punto p1 se mueve en una dirección aleatoria y el segundo punto p2 sigue la cola de p1 – trrrrrrm
Interesante, nunca lo hubiese entendido por su descripción del problema ... Y desafortunadamente, todavía estoy confundido. ¿Tienes un ejemplo visual de lo que quieres lograr? –
@ra_htial ¿Estás buscando algo como esto? http://jsfiddle.net/mobidevelop/bGgHQ/ (Mueva el mouse alrededor del lienzo) –