#codevember 11 - animated mandala
How do I make an #codevember 11 - animated mandala?
This is an animated mandala, created with random Bézier curves inside a circle. Particles with random colors are animated throughout the curves.. What is a #codevember 11 - animated mandala? How do you make a #codevember 11 - animated mandala? This script and codes were developed by Pedro Cacique on 20 November 2022, Sunday.
#codevember 11 - animated mandala - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>#codevember 11 - animated mandala</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <header> <button onclick="reset()" class="reset">Reset</button> </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/11">#day11</a> </div> </footer> <script src="js/index.js"></script>
</body>
</html>
#codevember 11 - animated mandala - Script Codes CSS Codes
body { margin: 0; font-family: Helvetica, Arial; font-weight: 100; overflow: hidden;
}
canvas { background-color: #fdfdfd;
}
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: flex-start; align-items: center;
}
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: #31dfff;
}
#codevember 11 - animated mandala - Script Codes JS Codes
//---------- MAIN FUNCTION ----------
function init() { for (var i = 0; i < 2 * Math.PI; i += step) { createElement(i); } for (var e = 0; e < elements.length; e++) { for (var i = 0; i < elements[e].path.length; i++) { elements[e].path[i].showPoints = false; elements[e].path[i].showLines = true; elements[e].path[i].draw(ctx); } } startAnimation();
}
function createElement(angle) { element = new Element(new Point(100, 100, { fillColor: getRandomColor(), lineColor: null, shape: "circle", size: 0 })); var sp = getPointOnCircle(center, circle_radius, angle); var ep = getPointOnCircle(center, circle_radius, Math.PI + angle); var radius = getRandomNumber(-circle_radius * 0.8, circle_radius * 0.8); var cp1 = getPointOnCircle2(center, radius, getRandomNumber(-radius, radius) + center.x); radius = getRandomNumber(circle_radius * 0.2, circle_radius * 0.8); var cp2 = getPointOnCircle2(center, radius, getRandomNumber(-radius, radius) + center.x); element.position.x = sp.x; element.position.y = sp.y; element.path.push(new BezierCurve(sp, cp1, cp2, ep)); elements.push(element);
}
function animate() { //ctx.clearRect(0, 0, cw, ch); for (var e = 0; e < elements.length; e++) { elements[e].animate(ctx); } requestAnimationFrame(animate);
}
//-----------------------------------
var canvas, ctx, cw, ch;
var FPS = 60, duration = 3;
var showPoints = false, showCurves = true;
var elements = [];
var circle_radius, center, step = 0.01;
var requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.msRequestAnimationFrame || function (callback) { return setTimeout(callback, FPS);
};
window.onload = function () { initAll(); animate();
}
window.onresize = function () { initCanvas(); ctx.clearRect(0, 0, cw, ch);
};
function initAll() { elements = []; 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; circle_radius = Math.min(cw, ch) * 0.4; center = new Point(cw / 2, ch / 2);
}
function startAnimation() { for (var i = 0; i < elements.length; i++) { elements[i].current = 0; elements[i].time = 0; elements[i].isAnimating = true; }
}
function reset() { initAll(); animate();
}
function getMousePos(canvas, evt) { var rect = canvas.getBoundingClientRect(); return new Point( evt.clientX - rect.left, evt.clientY - rect.top, 0 );
}
function getRandomNumber(min, max) { return Math.random() * (max - min + 1) + min;
}
function getPointOnCircle(center, radius, angle) { var x = center.x + radius * Math.cos(angle); var y = center.y + radius * Math.sin(angle); return new Point(x, y);
}
function getPointOnCircle2(center, radius, x) { var angle = Math.acos((x - center.x) / radius); var y = center.y + radius * Math.sin(angle); return new Point(x, y);
}
function componentToHex(c) { var hex = c.toString(16); return hex.length == 1 ? "0" + hex : hex;
}
function rgbToHex(r, g, b) { return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}
function getRandomColor() { var r = Math.floor(getRandomNumber(0, 255)); var g = Math.floor(getRandomNumber(0, 255)); var b = Math.floor(getRandomNumber(0, 255)); var a = getRandomNumber(0.2, 0.5); return "rgba(" + r + "," + g + "," + b + "," + a + ")";
}
//----------- 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 = 0.2; 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; this.duration = getRandomNumber(1, 5);
}
Element.prototype.draw = function (ctx) { this.position.draw(ctx);
}
Element.prototype.animate = function (ctx) { if (this.isAnimating) { this.position.size = 1.5; this.time += 1 / (this.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);
}
Developer | Pedro Cacique |
Username | phcacique |
Uploaded | November 20, 2022 |
Rating | 3 |
Size | 4,588 Kb |
Views | 18,216 |
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!
Name | Size |
One page scrolling | 2,113 Kb |
Grid Interaction | 3,469 Kb |
Page transition | 2,127 Kb |
Aside Hover Menu | 1,938 Kb |
Platform game mechanics | 2,799 Kb |
Web Theremin | 3,970 Kb |
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!
Name | Username | Size |
Basecamp 3 Document | Lachlanjc | 3,811 Kb |
Animated Slide Hamburger Mobile Menu | BJack | 2,247 Kb |
Simple jQuery Slider | Jurbank | 2,874 Kb |
Angular Route | Arun_v606 | 1,837 Kb |
Button shaking | SusanneLundblad | 2,227 Kb |
LBCA - Mail canvas | Emnbdx | 3,856 Kb |
Commuter Line Tokyu 8500 | Pedox | 7,031 Kb |
Michelle, submit your photography to Unsplash. | Zaneriley | 3,368 Kb |
OnScreenAction | Ozgursagiroglu | 2,647 Kb |
Kut D3 | Jellevrswk | 3,687 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!