JSFiddle - React, Tailwind, and code Playground
by f3ry
HTML
<canvas id="canvas" width=350 height=350></canvas>
CSS
body {
margin: 0;
}
canvas {
border:1px solid black;
}
JavaScript
(function () {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame'] || window[vendors[x] + 'CancelRequestAnimationFrame'];
}
if (!window.requestAnimationFrame) window.requestAnimationFrame = function (callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function () {
callback(currTime + timeToCall);
},
timeToCall);
lastTime = currTime + timeToCall;
return id;
};
if (!window.cancelAnimationFrame) window.cancelAnimationFrame = function (id) {
clearTimeout(id);
};
}());
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
ctx.lineCap = "round";
var t = 1;
var path = [];
path.push({
x: 0,
y: 0
});
path.push({
x: 200,
y: 50
});
path.push({
x: 110,
y: 200
});
path.push({
x: 30,
y: 90
});
path.push({
x: 70,
y: 20
});
function draw_line(){
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(0, 0);
for (var i = 1; i < path.length; i++){
ctx.lineTo(path[i].x, path[i].y);
}
ctx.stroke();
}
ctx.lineWidth = 5;
ctx.strokeStyle = "blue";
var points = path_to_points(path);
animate(points);
function path_to_points(path) {
var path_points = [];
for (var i = 1; i < path.length; i++) {
var pt0 = path[i - 1];
var pt1 = path[i];
var dx = pt1.x - pt0.x;
var dy = pt1.y - pt0.y;
for (var j = 0; j < 100; j++) {
var x = pt0.x + dx * j / 100;
var y = pt0.y + dy * j / 100;
path_points.push({
x: x,
y: y
});
}
...