Rotating Cube with p5.js

Developer
Size
2,148 Kb
Views
8,096

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 Previews

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"));
Rotating Cube with p5.js - Script Codes
Rotating Cube with p5.js - Script Codes
Home Page Home
Developer Ruslan Marin
Username marinru
Uploaded December 07, 2022
Rating 3
Size 2,148 Kb
Views 8,096
Do you need developer help for Rotating Cube with p5.js?

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!

Ruslan Marin (marinru) 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!