Skip to content

Instantly share code, notes, and snippets.

@benfoxall
Created October 14, 2016 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benfoxall/1036cd99d60d34018235e1aaeb0a513a to your computer and use it in GitHub Desktop.
Save benfoxall/1036cd99d60d34018235e1aaeb0a513a to your computer and use it in GitHub Desktop.
Paste this into cojs.co
import {ctx, w, h} from 'canvas'
import {loop, drawEdge, scale, rotateX, rotateY, rotateZ} from 'helpers'
const pM = $M([
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0.005,0.995]
])
const applyPerspective = v => {
const v4 = pM.x($V(v.elements.concat(1)))
return v4.x(v4.e(4))
}
/* * * * */
const points = [
[-10,-10,+10],
[-10,+10,+10],
[+10,+10,+10],
[+10,-10,+10],
[-10,-10,-10],
[-10,+10,-10],
[+10,+10,-10],
[+10,-10,-10]
].map(v => $V(v))
const [p1, p2, p3, p4, p5, p6, p7, p8] = points
const edges = [
[p1, p2], [p2, p3], [p3, p4], [p4, p1],
[p5, p6], [p6, p7], [p7, p8], [p8, p5],
[p1, p5], [p2, p6], [p3, p7], [p4, p8]
]
loop( time => {
const t = scale(3)
.multiply(rotateX(time/1000))
.multiply(rotateY(time/3000))
.multiply(rotateZ(time/7000))
;[{x: -100, r: -0.05},{x: 100, r: 0.05}]
.forEach( render => {
ctx.save()
ctx.translate(render.x,0)
const tc = rotateY(render.r).x(t)
edges
.map(e => e.map(p => applyPerspective(tc.multiply(p))))
.forEach(drawEdge)
ctx.restore()
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment