Rotating Cube with p5.js
How do I make an rotating cube with p5.js?
What is a rotating cube with p5.js? How do you make a rotating cube with p5.js? This script and codes were developed by Ruslan Marin on 07 December 2022, Wednesday.
Rotating Cube with p5.js - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Rotating Cube with p5.js</title>
</head>
<body> <div id="canvasContainer"></div> <script src='https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.6/p5.min.js'></script> <script src="js/index.js"></script>
</body>
</html>
Rotating Cube with p5.js - Script Codes JS Codes
var cube = {};
cube.nodes = [ {x:-100, y: -100, z: -100}, {x:-100, y: -100, z: 100}, {x:-100, y: 100, z: -100}, {x:-100, y: 100, z: 100}, {x:100, y: -100, z: -100}, {x:100, y: -100, z: 100}, {x:100, y: 100, z: -100}, {x:100, y: 100, z: 100} ];
cube.edges = [ { begin: cube.nodes[0], end: cube.nodes[1] }, { begin: cube.nodes[1], end: cube.nodes[3] }, { begin: cube.nodes[3], end: cube.nodes[2] }, { begin: cube.nodes[2], end: cube.nodes[0] }, { begin: cube.nodes[4], end: cube.nodes[5] }, { begin: cube.nodes[5], end: cube.nodes[7] }, { begin: cube.nodes[7], end: cube.nodes[6] }, { begin: cube.nodes[6], end: cube.nodes[4] }, { begin: cube.nodes[0], end: cube.nodes[4] }, { begin: cube.nodes[1], end: cube.nodes[5] }, { begin: cube.nodes[2], end: cube.nodes[6] }, { begin: cube.nodes[3], end: cube.nodes[7] }, { begin: cube.nodes[2], end: cube.nodes[5] }, { begin: cube.nodes[0], end: cube.nodes[7] }, { begin: cube.nodes[3], end: cube.nodes[4] }, { begin: cube.nodes[1], end: cube.nodes[6] }, ];
var rotate3DZ = function(figure, theta){ theta = -theta; for (var n = 0; n < figure.nodes.length; n++) { var x = figure.nodes[n].x; var y = figure.nodes[n].y; figure.nodes[n].x = x * Math.cos(theta) - y * Math.sin(theta); figure.nodes[n].y = y * Math.cos(theta) + x * Math.sin(theta); }
};
var rotate3DX = function(figure, theta){ theta = -theta; for (var n = 0; n < figure.nodes.length; n++) { var y = figure.nodes[n].y; var z = figure.nodes[n].z; figure.nodes[n].y = y * Math.cos(theta) - z * Math.sin(theta); figure.nodes[n].z = z * Math.cos(theta) + y * Math.sin(theta); }
};
var rotate3DY = function(figure, theta){ theta = -theta; for (var n = 0; n < figure.nodes.length; n++) { var x = figure.nodes[n].x; var z = figure.nodes[n].z; figure.nodes[n].x = x * Math.cos(theta) - z * Math.sin(theta); figure.nodes[n].z = z * Math.cos(theta) + x * Math.sin(theta); }
};
var sketchProc = function(p) { var backgroundColor; var nodeColor; var edgeColor; var nodeSize; p.setup = function() { backgroundColor = p.color(0, 0, 1); nodeColor = p.color(40, 168, 107); edgeColor = p.color(34, 68, 204); nodeSize = 8; p.frameRate(30); p.createCanvas(400,400); p.textSize(18); p.textAlign(p.CENTER,p.CENTER); }; // BEGIN Draw Loop p.draw = function(){ p.background(backgroundColor); p.translate(p.width/2, p.width/2); p.stroke(edgeColor); for (n = 0; n < cube.edges.length; n++) { p.line(cube.edges[n].begin.x, cube.edges[n].begin.y, cube.edges[n].end.x, cube.edges[n].end.y); } p.fill(nodeColor); p.noStroke(); for (var n = 0; n < cube.nodes.length; n++) { //p.ellipse(cube.nodes[n].x, cube.nodes[n].y, nodeSize, nodeSize); p.fill(255); p.text(n, cube.nodes[n].x, cube.nodes[n].y); } rotate3DZ(cube, 0.03); rotate3DX(cube, 0.01); rotate3DY(cube, 0.04); }; // END Draw Loop
};
new p5(sketchProc, document.getElementById("canvasContainer"));
Developer | Ruslan Marin |
Username | marinru |
Uploaded | December 07, 2022 |
Rating | 3 |
Size | 2,148 Kb |
Views | 8,096 |
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 |
Vue.js Basic Component | 1,566 Kb |
Vue.js Conditional Visibility | 1,520 Kb |
KO Components Demo | 2,994 Kb |
A Pen by Ruslan Marin | 1,561 Kb |
P5.js Clock | 2,414 Kb |
Knockout Lists | 2,531 Kb |
Sidescroller with p5.js | 2,455 Kb |
Component Props | 3,113 Kb |
Phone Call Links | 1,217 Kb |
Vue.js User Input | 1,565 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 |
Spin | Elalemanyo | 8,262 Kb |
CSS Link Icons with jQuery Titles | Nicwinn | 2,312 Kb |
Ipad Mini Showcase | Hornebom | 10,479 Kb |
Another brick in the wall | Fivera | 1,955 Kb |
Colorful Sliders | Chanrith | 1,246 Kb |
A Pen by Sooba | Sooba | 2,516 Kb |
Simple Flat Menu | Jeplaa | 2,467 Kb |
Particle Motion trajectories | Sniejadlik | 5,899 Kb |
To Do List with Delete | Mattlbrody | 2,068 Kb |
Practice using Wixel | Emnk | 3,057 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!