Skip to content

Instantly share code, notes, and snippets.

@CaterpyOwO
Created November 1, 2020 19:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CaterpyOwO/70a1a0f3a087aab34e3305c2618e7441 to your computer and use it in GitHub Desktop.
Save CaterpyOwO/70a1a0f3a087aab34e3305c2618e7441 to your computer and use it in GitHub Desktop.
A rotating 3d cube in JavaScript
{let e=document,t=e.createElement("canvas"),
n=t.getContext("2d"),i={},h=[[-1,-1,-1],[1,-
1,-1],[1,1,-1],[-1,1,-1],[-1,-1,1],[1,-1,1],
[1,1,1],[-1,1,1]],o=[0,1,1,2,2,3,3,0,4,5,5,6
,6,7,7,4,0,4,1,5,2,6,3,7],r=(e,t=0)=>{let[n,
i]=e,h=Math.sin(t),o=Math.cos(t);return[n*o-
i*h,i*o+n*h]},a=e=>{let n,[h,o,a]=e;return[h
,a]=r([h,a],i.y),[o,a]=r([o,a],i.x),a-=-5,[h
,a]=r([h,a]),[o,a]=r([o,a]),h*=n=/*made by*/
600/Math.max(0,a),o*=n,[t.width/2+h,t.height
/2+o]},d=()=>{requestAnimationFrame(d);let e
,r; let l=Date.now();i.x=5e-4*l,i.y=7e-4*l;n
.clearRect(0,0,t.width,t.height);for(let t=o
.length/2;t--;)n.lineWidth=2,n.beginPath(),[
e,r]=a(h[o[2*t]]),n.moveTo(e,r),[e,r]=a(h[o[
1+2*t]]),n.lineTo(e,r),n.stroke()};let q="m\
argin:0;overflow:hidden";e.body.innerHTML=""
,e.body.style=q,t.width=innerWidth,t.height=
innerHeight,e.body.append(t),d()}/*caterpy*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment