Cutaway hemisphere - js canvas 2D

Developer
Size
4,111 Kb
Views
18,216

How do I make an cutaway hemisphere - js canvas 2d?

Started off as something else but now its a marble earth kind of thing. will be my star-raiders starbase. What is a cutaway hemisphere - js canvas 2d? How do you make a cutaway hemisphere - js canvas 2d? This script and codes were developed by Andi Smithers on 21 November 2022, Monday.

Cutaway hemisphere - js canvas 2D Previews

Cutaway hemisphere - js canvas 2D - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Cutaway hemisphere - js canvas 2D</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <body> <canvas id='hemisphere'></canvas>
</body> <script src="js/index.js"></script>
</body>
</html>

Cutaway hemisphere - js canvas 2D - Script Codes CSS Codes

body{ background:#000000; margin: 0px; padding: 0px;
}

Cutaway hemisphere - js canvas 2D - Script Codes JS Codes

/*****************************************************************************
The MIT License (MIT)
Copyright (c) 2014 Andi Smithers
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*****************************************************************************/
// conceptualized and written by andi smithers
// constant options
const focalDepth = 80;
const focalPoint = 256;
// variables
var centreX;
var centreY;
var mouseX;
var mouseY;
var spawnX;
var spawnY;
var frameCount=0;
var targetLayer = 0.25;
var layer = 0.25;
// test multiple groups
// initialization
function init()
{ // setup canvas and context	canvas = document.getElementById('hemisphere');	context = canvas.getContext('2d'); // set canvas to be window dimensions resize(); // create event listeners canvas.addEventListener('mousemove', mouseMove); canvas.addEventListener('click', mouseClick); window.addEventListener('resize', resize); // initialze variables
}
// input functions
function mouseMove(event)
{	var rect = canvas.getBoundingClientRect();	mouseX = event.clientX - rect.left,	mouseY = event.clientY - rect.top
}
function mouseClick()
{ targetLayer-=0.25; if (targetLayer <0.0) targetLayer = 0.75;
}
function resize()
{ canvas.width = window.innerWidth; canvas.height = window.innerHeight; // compute centre of screen centreX = canvas.width/2; centreY = canvas.height/2;
}
// rendering functions
function render()
{ context.fillStyle = 'black'; context.clearRect(0, 0, canvas.width, canvas.height); RenderBlueMarble(); context.globalAlpha = 1.0; context.font = '20pt Calibri'; context.fillStyle = 'rgb(255,255,255)'; context.textAlign = "center"; context.fillText('Hemi-Sphere 2D rendering', canvas.width/2, 20); context.fillText('Blue Marble Cutaway', canvas.width/2, canvas.height-20); context.font = '14pt Calibri'; context.fillText('(click to cycle layers)', canvas.width/2, 40);
}
function RenderBlueMarble()
{ // interporlate layer changes so its nice and smooth var dx = targetLayer-layer; layer+=dx*0.1; // rotate angle=(angle+0.01)%(Math.PI*2); var x = centreX; var y = centreY; var radius = canvas.height/4; if (angle>Math.PI*0.5 && angle<Math.PI*1.5) RenderCore(x, y, radius*layer, layer); RenderHemisphere(x, y, radius, 4, '#0000ff'); if (!(angle>Math.PI*0.5 && angle<Math.PI*1.5)) RenderCore(x, y, radius*layer, layer);
}
function RenderCore(x, y, r, layer)
{ var outerColor = 'rgb(255,'+Math.floor(255-255*layer)+',0)'; angle=(angle+Math.PI)%(Math.PI*2); RenderHemisphere(x, y, r, 1, outerColor); angle=(angle+Math.PI)%(Math.PI*2);
}
// star base construction - eer well it was but its a cool blue marble demo for now
// to rotate horizontally switch out the x/y registers. I should probably make this generic .. however just want to use this for the starbase construction in star-raiders
var angle = 0;
function RenderHemisphere(x, y, rad, slices, outerColor)
{ // really bad hack to change orientation of the hemisphere // rotate the object 90 degrees and then compensate for the centre x/y changes context.save(); context.rotate(Math.PI*0.5); context.translate(-x+y,-y-x); m = 0.5522848; // molten core for (var r=rad; r>0; r-=(rad/slices)) { tr =r*Math.cos(angle); // top hemisphere angle *rad br =r*Math.cos(angle); // bottom angle *rad xkr = m*r; // kappa * r tkr = m*tr; // top height * kappa bkr = m*br; // bottom height * kappa context.beginPath() context.moveTo(x+r, y) context.bezierCurveTo(x+r, y-tkr, x+xkr, y-tr, x, y-tr) // top r context.bezierCurveTo(x-xkr, y-tr, x-r, y-tkr, x-r, y) // top l context.bezierCurveTo(x-r, y+bkr, x-xkr, y+br, x, y+br) // bot l context.bezierCurveTo(x+xkr, y+br, x+r, y+bkr, x+r, y) // bot r context.closePath(); context.lineWidth = 1; context.fillStyle = 'rgb(255,' + Math.floor(255-r/rad*255) +',0)'; context.fill(); } // drow the blue surface var r = rad; tr =r*Math.cos(angle+Math.PI); // top hemisphere angle *rad br =r*Math.cos(angle+Math.PI); // bottom angle *rad if (angle<Math.PI) br = r; if (angle>Math.PI) tr = r; xkr = m*r; // kappa * r tkr = m*tr; // top height * kappa bkr = m*br; // bottom height * kappa context.beginPath() context.moveTo(x+r, y) context.bezierCurveTo(x+r, y-tkr, x+xkr, y-tr, x, y-tr) // top r context.bezierCurveTo(x-xkr, y-tr, x-r, y-tkr, x-r, y) // top l context.bezierCurveTo(x-r, y+bkr, x-xkr, y+br, x, y+br) // bot l context.bezierCurveTo(x+xkr, y+br, x+r, y+bkr, x+r, y) // bot r context.closePath(); context.lineWidth = 1; context.fillStyle = outerColor; context.fill(); context.restore();
}
// movement functions
function update()
{
}
// per frame tick functions
function animate()
{ frameCount++; // movement update update(); // render update render(); // trigger next frame requestAnimationFrame(animate);
}
// entry point
init();
animate();
Cutaway hemisphere - js canvas 2D - Script Codes
Cutaway hemisphere - js canvas 2D - Script Codes
Home Page Home
Developer Andi Smithers
Username Bolloxim
Uploaded November 21, 2022
Rating 3.5
Size 4,111 Kb
Views 18,216
Do you need developer help for Cutaway hemisphere - js canvas 2D?

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!

Andi Smithers (Bolloxim) Script Codes
Create amazing blog posts 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!