#codevember 9 - Bezier Curve Path Animation

Developer
Size
4,652 Kb
Views
22,264

How do I make an #codevember 9 - bezier curve path animation?

Animation throughout a path made by bezier curves. What is a #codevember 9 - bezier curve path animation? How do you make a #codevember 9 - bezier curve path animation? This script and codes were developed by Pedro Cacique on 20 November 2022, Sunday.

#codevember 9 - Bezier Curve Path Animation Previews

#codevember 9 - Bezier Curve Path Animation - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>#codevember 9 - Bezier Curve Path Animation</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <header> <div> <p> <input type="checkbox" id="sp" checked/> <label for="sp">Show Points</label> </p> <p> <input type="checkbox" id="sc" checked/> <label for="sc">Show Curves</label> </p> <p>Speed: <br> <input type="number" id="duration" value="1" /> </p> <p> <button id="butAnimate" onclick="startAnimation()">Animate Element</button> <button id="butAnimate" onclick="reset()" class="reset">Reset</button> </p> </div> <p> Double click to add curve </p> </header> <canvas id="canvas"></canvas> <footer> <div>Made by <a href="http://www.pedrocacique.com">Cacique</a></div> <div> <a href="http://codevember.xyz/about">#codevember</a> <a href="http://codevember.xyz/day/9">#day9</a> </div> </footer> <script src="js/index.js"></script>
</body>
</html>

#codevember 9 - Bezier Curve Path Animation - Script Codes CSS Codes

body { margin: 0; font-family: Helvetica, Arial; font-weight: 100; overflow: hidden;
}
canvas { background-color: #101010;
}
footer { position: fixed; bottom: 0; height: 20px; width: 100%; display: flex; justify-content: space-around; align-items: center; background-color: #202020; color: #fff; font-size: 0.7em;
}
footer a { color: #f0f0f0;
}
header { color: #fff; padding: 10px; position: fixed; width: 100%; display: flex; justify-content: center; align-items: center; flex-flow: column wrap;
}
header div{ width: 100%; display: flex; justify-content: center; align-items: center;
}
header p { margin: 10px;
}
input[type="number"] { padding: 5px; border: none; border-radius: 4px; width: 55px;
}
button { border: none; border-radius: 4px; padding: 5px; background-color: #f99500;
}
button:focus { outline: none;
}
.reset { background-color: #d85656;
}

#codevember 9 - Bezier Curve Path Animation - Script Codes JS Codes

//---------- MAIN FUNCTION ----------
function init() { element = new Element(new Point(100, 100, { fillColor: "#52c93a", lineColor: null, shape: "circle", size: 20 })); element.path.push(new BezierCurve(new Point(120, 100), new Point(150, 220), new Point(300, 200), new Point(300, 300)));
}
function animate() { ctx.clearRect(0, 0, cw, ch); if (showCurves) { for (var i = 0; i < element.path.length; i++) { element.path[i].draw(ctx); element.path[i].showPoints = showPoints; element.path[i].showLines = showPoints; } } element.animate(ctx); requestAnimationFrame(animate);
}
//-----------------------------------
var canvas, ctx, cw, ch;
var draggingCurves, draggingPoints;
var FPS = 60,   duration = 3, isAnimating = false;
var showPoints = true, showCurves = true;
var element;
var requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.msRequestAnimationFrame || function (callback) { return setTimeout(callback, FPS);
};
window.onload = function () { initAll(); animate(); document.getElementById('sp').addEventListener('change', function () { showPoints = document.getElementById('sp').checked; }); document.getElementById('sc').addEventListener('change', function () { showCurves = document.getElementById('sc').checked; });
}
window.onresize = function () { initCanvas(); ctx.clearRect(0, 0, cw, ch);
};
function initAll() { initCanvas(); init(); ctx.clearRect(0, 0, cw, ch);
}
function initCanvas() { canvas = document.getElementById('canvas'); ctx = canvas.getContext('2d'); cw = window.innerWidth; ch = window.innerHeight; canvas.width = cw; canvas.height = ch; canvas.addEventListener("mousedown", onMouseDown); canvas.addEventListener("mouseup", onMouseUp); canvas.addEventListener("dblclick", onDblClick);
}
function onDblClick(evt) { var p1 = element.path[element.path.length - 1].endPoint.clone(); var lcp2 = element.path[element.path.length - 1].controlPoint2.clone(); var p2 = getMousePos(canvas, evt); cp1 = new Point(p1.x + (p2.x - p1.x) / 2, p1.y + (p1.y - lcp2.y)); cp2 = new Point(p1.x + (p2.x - p1.x) / 2, p2.y); element.path.push(new BezierCurve(p1, cp1, cp2, p2));
}
function startAnimation(evt) {  element.current = 0; duration = parseInt(document.getElementById('duration').value); element.time = 0; element.isAnimating = true;
}
function reset() { initAll(); animate();
}
function onMouseDown(evt) { var p = getMousePos(canvas, evt); var points = ["startPoint", "endPoint", "controlPoint1", "controlPoint2"]; draggingCurves = []; draggingPoints = []; for (var i = 0; i < element.path.length; i++) { for (var j = 0; j < points.length; j++) { if (p.near(element.path[i][points[j]], element.path[i][points[j]].size)) { draggingCurves.push(i); draggingPoints.push(points[j]); } } } if (draggingCurves.length != -1) canvas.addEventListener("mousemove", onMouseMove);
}
function onMouseUp(evt) { canvas.removeEventListener("mousemove", onMouseMove);
}
function onMouseMove(evt) { var p = getMousePos(canvas, evt); for (var i = 0; i < draggingCurves.length; i++) { element.path[draggingCurves[i]][draggingPoints[i]].x = p.x; element.path[draggingCurves[i]][draggingPoints[i]].y = p.y; }
}
function getMousePos(canvas, evt) { var rect = canvas.getBoundingClientRect(); return new Point( evt.clientX - rect.left, evt.clientY - rect.top, 0 );
}
//----------- Prototypes -------------
//----------- POINT -----------
function Point(x, y, properties) { this.x = (x == null) ? 0 : x; this.y = (y == null) ? 0 : y; this.lineColor = "#fff"; this.fillColor = null; this.shape = "square"; this.size = 5; this.time = 0; this.lineWidth = 1; if (properties != null) this.setProperties(properties);
}
Point.prototype.distance = function (p2) { //Euclidean Disctance return Math.sqrt(Math.pow(this.x - p2.x, 2) + Math.pow(this.y - p2.y, 2) + Math.pow(this.z - p2.z, 2));
}
Point.prototype.manhatan = function (p2) { return (p2.x - p1.x) + (p2.y - p1.y) + (p2.z - p1.z);
}
Point.prototype.setProperties = function (properties) { for (var p in properties) { this[p] = properties[p]; }
}
Point.prototype.clone = function () { var copy = new Point(); for (var attr in this) { if (this.hasOwnProperty(attr)) copy[attr] = this[attr]; } return copy;
}
Point.prototype.equals = function (p2) { return (this.x == p2.x && this.y == p2.y);
}
Point.prototype.near = function (p2, offset) { return (this.x > p2.x - offset && this.x < p2.x + offset && this.y > p2.y - offset && this.y < p2.y + offset)
}
Point.prototype.draw = function (ctx) { ctx.beginPath(); if (this.lineColor != null) { ctx.strokeStyle = this.lineColor; ctx.lineWidth = this.lineWidth; } if (this.fillColor == null) this.fillColor = "rgba(0,0,0,0)"; ctx.fillStyle = this.fillColor; if (this.shape == "circle") ctx.arc(this.x, this.y, this.size / 2, 0, Math.PI * 2); else ctx.rect(this.x - this.size / 2, this.y - this.size / 2, this.size, this.size); if (this.lineColor != null) ctx.stroke(); ctx.fill();
}
Point.prototype.trace = function () { console.log("(" + this.x + ", " + this.y + ")");
}
//----------- RECT -----------
function Rect(p1, p2, properties) { this.p1 = (p1 == null) ? new Point() : p1; this.p2 = (p2 == null) ? new Point(10, 0) : p2; this.lineColor = "#fff"; this.lineWidth = 1; if (properties != null) this.setProperties(properties);
}
Rect.prototype.setProperties = function (properties) { for (var p in properties) { this[p] = properties[p]; }
}
Rect.prototype.clone = function () { var copy = new Rect(); for (var attr in this) { if (this.hasOwnProperty(attr)) copy[attr] = this[attr]; } return copy;
}
Rect.prototype.draw = function (ctx) { ctx.beginPath(); ctx.lineWidth = this.lineWidth; ctx.strokeStyle = this.lineColor; ctx.moveTo(this.p1.x, this.p1.y); ctx.lineTo(this.p2.x, this.p2.y); ctx.stroke();
}
Rect.prototype.trace = function () { console.log(this);
}
//----------- BEZIER CURVE -----------
function BezierCurve(startPoint, controlPoint1, controlPoint2, endPoint, properties) { this.startPoint = (startPoint != null) ? startPoint : (new Point(100, 200)); this.endPoint = (endPoint != null) ? endPoint : (new Point(200, 100)); this.controlPoint1 = (controlPoint1 != null) ? controlPoint1 : (new Point(150, 200)); this.controlPoint2 = (controlPoint2 != null) ? controlPoint2 : (new Point(200, 150)); this.color = "#fff"; this.lineWidth = 1; this.showLines = true; this.showPoints = true; var prop1 = { shape: "circle", lineColor: "#ffd460", fillColor: "#ffd460", size: 8 }; var prop1b = { shape: "circle", lineColor: "#ffd460", size: 8 }; var prop2 = { shape: "square", lineColor: "#68ccff", fillColor: "#68ccff", size: 8 }; this.startPoint.setProperties(prop1b); this.endPoint.setProperties(prop1); this.controlPoint1.setProperties(prop2); this.controlPoint2.setProperties(prop2); if (properties != null) this.setProperties(properties);
}
BezierCurve.prototype.draw = function (ctx) { if (this.showLines) { this.startPoint.draw(ctx); this.endPoint.draw(ctx); this.controlPoint1.draw(ctx); this.controlPoint2.draw(ctx); if (this.showPoints) { var r1 = new Rect(this.startPoint, this.controlPoint1, { lineColor: this.controlPoint1.lineColor, lineWidth: this.lineWidth / 2 }); r1.draw(ctx); var r2 = new Rect(this.endPoint, this.controlPoint2, { lineColor: this.controlPoint2.lineColor, lineWidth: this.lineWidth / 2 }); r2.draw(ctx); } } ctx.beginPath(); ctx.strokeStyle = this.color; ctx.lineWidth = this.lineWidth; ctx.moveTo(this.startPoint.x, this.startPoint.y); ctx.bezierCurveTo(this.controlPoint1.x, this.controlPoint1.y, this.controlPoint2.x, this.controlPoint2.y, this.endPoint.x, this.endPoint.y); ctx.stroke();
}
BezierCurve.prototype.setProperties = function (properties) { for (var p in properties) { this[p] = properties[p]; }
}
BezierCurve.prototype.clone = function () { var copy = new BezierCurve(); for (var attr in this) { if (this.hasOwnProperty(attr)) copy[attr] = this[attr]; } return copy;
}
BezierCurve.prototype.trace = function () { console.log(this);
}
BezierCurve.prototype.getPos = function (t) { var point = new Point(); t2 = t * t t3 = t2 * t mt = 1 - t mt2 = mt * mt mt3 = mt2 * mt point.x = mt3 * this.startPoint.x + 3 * mt2 * t * this.controlPoint1.x + 3 * mt * t2 * this.controlPoint2.x + t3 * this.endPoint.x; point.y = mt3 * this.startPoint.y + 3 * mt2 * t * this.controlPoint1.y + 3 * mt * t2 * this.controlPoint2.y + t3 * this.endPoint.y; return point;
}
//------------------ ELEMENT ------------------
function Element(position, path) { this.position = (position != null) ? position : (new Point(100, 100)); this.path = []; if (path != null) this.path = path; this.isAnimating = false; this.current = 0;
}
Element.prototype.draw = function (ctx) { this.position.draw(ctx);
}
Element.prototype.animate = function (ctx) { if (this.isAnimating) {    this.time += 1 / (duration * FPS); if (this.time >= 1) { if (this.current < this.path.length - 1) { this.time = 0; this.current++; } else { this.isAnimating = false; this.time = 1; } } var p = this.path[this.current].getPos(this.time); this.position.x = p.x; this.position.y = p.y; } this.draw(ctx);
}
#codevember 9 - Bezier Curve Path Animation - Script Codes
#codevember 9 - Bezier Curve Path Animation - Script Codes
Home Page Home
Developer Pedro Cacique
Username phcacique
Uploaded November 20, 2022
Rating 3
Size 4,652 Kb
Views 22,264
Do you need developer help for #codevember 9 - Bezier Curve Path Animation?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Pedro Cacique (phcacique) Script Codes
Create amazing sales emails with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!