Last active
September 7, 2021 04:56
-
-
Save JAForbes/6e2c2c183c68ce924eef to your computer and use it in GitHub Desktop.
spaceship.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.body.innerText = "" | |
can = document.createElement('canvas') | |
con = can.getContext('2d') | |
document.body.appendChild(can) | |
can.width = can.height = 600 | |
con.translate(300,300) | |
var cap = { | |
x:0, y:-41, | |
scale: {x:8,y:8}, | |
points: [ | |
[0,-1], | |
[1,1], | |
[0.5,0.5], | |
[-0.5,0.5], | |
[-1,1], | |
[0,-1] | |
], | |
fill: 'rgb(70,70,70)', | |
} | |
var cockpit = { | |
x:0, y:-30, | |
scale: {x:16,y:18}, | |
points: [ | |
[0,-1], | |
[1,1], | |
[0.5,1.5], | |
[-0.5,1.5], | |
[-1,1], | |
[0,-1] | |
], | |
fill: 'rgba(80,200,255,0.5)', | |
} | |
var join = { | |
x:0, y:3.5, | |
scale: {x:20,y:-4}, | |
points: [ | |
[0,-1], | |
[0.5,1], | |
[-0.5,1], | |
[0,-1] | |
], | |
fill: 'rgb(150,150,150)', | |
} | |
var cockpitHL = { | |
x:0, y:-25, | |
scale: {x:10,y:12}, | |
points: [ | |
[0,-2], | |
[-0.8,1.85], | |
[-1.6,1.10], | |
[0,-1] | |
], | |
fill: 'rgba(255,255,255,0.5)', | |
} | |
var cockpitShadow = { | |
x:0, y:-25, | |
scale: {x:-10,y:12}, | |
points: [ | |
[0,-2], | |
[-0.8,1.85], | |
[-1.6,1.10], | |
[0,-1] | |
], | |
fill: 'rgba(0,0,0,0.2)', | |
} | |
var nose= { | |
x:0, y:-30, | |
scale: {x:20,y:20}, | |
points: [ | |
[0,-1], | |
[1,1], | |
[0.5,1.5], | |
[-0.5,1.5], | |
[-1,1], | |
[0,-1] | |
], | |
fill: 'rgb(70,70,70)', | |
} | |
var engineRimRight = { | |
x:0, y:0, | |
scale: {x:-50,y:50}, | |
points: [ | |
[0,-1], | |
[0,0.7], | |
[-1,1], | |
[0,-1] | |
], | |
fill: 'rgb(200,200,200)', | |
} | |
var engineRimLeft = { | |
x:0, y:0, | |
scale: {x:50,y:50}, | |
points: [ | |
[0,-1], | |
[0,0.7], | |
[-1,1], | |
[0,-1] | |
], | |
fill: 'rgb(200,200,200)', | |
} | |
var hullRight = { | |
x:0, y:0, | |
scale: {x:-50,y:50}, | |
points: [ | |
[0,-1], | |
[0,0.5], | |
[-0.9,0.8], | |
[0,-1] | |
], | |
fill: 'rgb(50,50,50)', | |
} | |
var hullLeft = { | |
x:0, y:0, | |
scale: {x:50,y:50}, | |
points: [ | |
[0,-1], | |
[0,0.5], | |
[-0.9,0.8], | |
[0,-1] | |
], | |
fill: 'grey', | |
} | |
var floor = { | |
x:0, y:0, | |
scale: {x:50,y:50}, | |
points: [ | |
[0,-1], | |
[1,1], | |
[0,1.2], | |
[-1,1], | |
[0,-1] | |
], | |
fill: 'black', | |
} | |
var flame = { | |
x: 0, y:40, | |
scale: {x:30,y:-25}, | |
points: [ | |
[0,-1], | |
[1,1], | |
[-1,1], | |
[0,-1] | |
], | |
fill: 'rgba(255,50,100,0.9)', | |
} | |
var flame2 = { | |
x: 0, y:50, | |
scale: {x:35,y:-25}, | |
points: [ | |
[0,-1], | |
[1,1], | |
[-1,1], | |
[0,-1] | |
], | |
fill: 'rgba(255,50,0,0.9)', | |
} | |
var glow = { | |
x: 0, y:55, | |
scale: {x:38,y:-30}, | |
points: [ | |
[0,-1], | |
[1,1], | |
[-1,1], | |
[0,-1] | |
], | |
fill: 'rgba(255,200,100,0.4)', | |
} | |
offset = function(offset){ | |
return function(point){ | |
return [ | |
point[0]+offset.x, | |
point[1]+offset.y | |
] | |
} | |
} | |
scale = function(scale, points){ | |
return points.map(function(point){ | |
return [ | |
point[0] * scale.x || 1, | |
point[1] * scale.y || 1 | |
] | |
}) | |
} | |
draw = function(obj){ | |
con.beginPath() | |
var points = scale(obj.scale,obj.points) | |
.map(offset(obj)) | |
con.moveTo.apply(con,points[0]) | |
points.slice(1,-1).map(function(point){ | |
console.log(point) | |
con.lineTo.apply(con,point) | |
}) | |
obj.fill && (con.fillStyle = obj.fill) && con.fill() | |
con.closePath() | |
obj.stroke && (con.strokeStyle = obj.stroke) && con.stroke() | |
} | |
draw(floor) | |
draw(flame2) | |
draw(flame) | |
draw(glow) | |
draw(engineRimLeft) | |
draw(engineRimRight) | |
draw(hullLeft) | |
draw(hullRight) | |
draw(nose) | |
draw(cockpit) | |
draw(join) | |
draw(cockpitHL) | |
draw(cockpitShadow ) | |
draw(cap) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment