Landscape

Developer
Size
3,792 Kb
Views
24,288

How do I make an landscape?

A little generative landscape. Play with values to obtain different shapes and forms. What is a landscape? How do you make a landscape? This script and codes were developed by Endre Simo on 28 November 2022, Monday.

Landscape Previews

Landscape - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Landscape</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div class="context"></div>
<div id="gui"></div> <script src='https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.5/dat.gui.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

Landscape - Script Codes CSS Codes

html, body { background: #111; margin: 0; padding: 0; -webkit-user-select: none; -moz-user-select: none; -khtml-user-select: none; user-select: none;
}
.context #canvas { position: relative; width: 100%; height: 100%;
}
#gui { position: relative; margin: 0 auto; width: 22%;
}

Landscape - Script Codes JS Codes

(function() { var lastTime = 0; var vendors = ['ms', 'moz', 'webkit', 'o']; for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) { window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame']; window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelRequestAnimationFrame']; } if (!window.requestAnimationFrame) window.requestAnimationFrame = function(callback, element) { var currTime = new Date().getTime(); var timeToCall = Math.max(0, 16 - (currTime - lastTime)); var id = window.setTimeout(function() { callback(currTime + timeToCall); }, timeToCall); lastTime = currTime + timeToCall; return id; }; if (!window.cancelAnimationFrame) window.cancelAnimationFrame = function(id) { clearTimeout(id); };
}());
function Landscape() { var canvas = document.createElement('canvas'); canvas.setAttribute('width', '1200'); canvas.setAttribute('height', '600'); canvas.setAttribute('id', 'canvas'); var mainContext = document.getElementsByClassName('context')[0]; mainContext.appendChild(canvas); var ctx = canvas.getContext('2d'); var gradient; // Global variables this.centerZ = GUI.centerZ; this.focalLength = GUI.focalLength; this.zIndex = GUI.zIndex; this.zRes = GUI.zRes; this.color = GUI.color; this.canvas = canvas; this.hexToRgb = function(hex) { // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF") var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i; hex = hex.replace(shorthandRegex, function(m, r, g, b) { return r + r + g + g + b + b; }); var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); return result ? { r: parseInt(result[1], 16), g: parseInt(result[2], 16), b: parseInt(result[3], 16) } : null; }; this.init = function() { this.width = canvas.width; this.height = canvas.height; this.centerX = this.width / 2; this.centerY = this.height / 2; this.timer = null; var radius = 0.7 * Math.sqrt(Math.pow(this.width, 2), Math.pow(this.height), 2); gradient = ctx.createRadialGradient(this.width * .5, this.height * .5, this.width * .25, this.width * .5, this.height *.5, radius); gradient.addColorStop(0, '#E2E2E2'); gradient.addColorStop(1, 'rgba(190,190,190, 0.8)'); ctx.fillStyle = gradient; ctx.fillRect(0,0,this.width, this.height); ctx.moveTo(0,0); }; this.reset = function() { this.centerZ = GUI.centerZ; this.focalLength = GUI.focalLength; this.zIndex = GUI.zIndex; this.zRes = GUI.zRes; this.color = GUI.color; ctx.fillStyle = gradient; ctx.clearRect(0,0,this.width,this.height); ctx.fillRect(0,0,this.width,this.height); } this.draw = function() { var self = this; this.drawLine(); this.timer = window.requestAnimationFrame(function() { self.draw(); }); }; this.drawLine = function() { if(this.zIndex > -this.focalLength) { var scale = this.focalLength / (this.focalLength + this.zIndex); var x1 = this.centerX - this.width / scale >> 0 * 0.2; var x2 = this.centerX + this.width / scale >> 0 * 0.2; var y = this.centerY + (Math.random() * GUI.roughness + 100) * scale; ctx.beginPath(); ctx.strokeStyle = 'rgba(' + this.hexToRgb(GUI.color).r + ',' + this.hexToRgb(GUI.color).g + ',' + this.hexToRgb(GUI.color).b + ', 0.15)'; ctx.moveTo(x1, y); for (var x = x1; x < x2; x += GUI.zRes) { var y = this.centerY + (Math.random() * GUI.roughness + 100) * scale ctx.lineTo(x, this.getLineDistance(x, this.zIndex / y * GUI.distanceField) * scale); } //ctx.lineTo(x, this.height); //ctx.lineTo(0, this.height); ctx.stroke(); ctx.closePath(); this.zIndex -= this.zRes; } else { window.cancelAnimationFrame(this.timer); } }; this.getLineDistance = function (x, z) { var x1 = Math.abs(x - this.centerX); var z1 = Math.abs(z - this.centerZ); var dist1 = Math.sqrt(x1 * x1 + z1 * z1); var x2 = Math.abs(x1 - this.centerX); var z2 = Math.abs(z1 - this.centerZ); var dist2 = Math.sqrt(x2 * x2 + z2 * z2); var angle = Math.atan2(z2, x2); return this.centerY + GUI.centerY + Math.sin(dist1 * GUI.xFactor) * (angle * 180 / Math.PI) + Math.cos(dist2 * GUI.yFactor) * (angle * 180 / Math.PI) - (this.zIndex - dist2) / dist1 + Math.random() * 0.5; };
};
var GUI = new function() { this.centerZ = 350; this.centerY = 500; this.focalLength = 400; this.zIndex = 3500; this.zRes = 6; this.color = "#000"; this.roughness = 3; this.distanceField = 75; this.xFactor = 0.07; this.yFactor = 0.02; return this;
}
var gui = new dat.GUI({autoPlace:false, width: 220, align: "left"});
gui.domElement.style.position = 'absolute';
gui.domElement.style.left = '0';
gui.domElement.style.top = '0';
var datGUI = document.getElementById('gui');
console.log(datGUI);
datGUI.parentNode.insertBefore(gui.domElement, datGUI);
var landscape = new Landscape();
var gs = gui.addFolder('General settings');
gs.add(GUI,'focalLength').min(250).max(500).step(10).name('Focal Length').onChange(function(value){landscape.reset()});
gs.add(GUI,'centerZ').min(100).max(400).step(50).name('Z Center').onChange(function(value){landscape.reset()});
gs.add(GUI,'centerY').min(200).max(600).step(50).name('Y Center').onChange(function(value){landscape.reset()});
gs.add(GUI,'zIndex').min(3000).max(5000).step(100).name('Z index').onChange(function(value){landscape.reset()});
gs.add(GUI,'zRes').min(2).max(10).step(1).name('Resolution').onChange(function(value){ zRes = value; landscape.reset()});
gs.addColor(GUI, 'color').name('Color').onChange(function(value){landscape.reset()});
var pattern = gui.addFolder('Pattern settings');
pattern.add(GUI,'roughness').min(0).max(25).step(1).name('Line Roughness').onChange(function(value){landscape.reset()});
pattern.add(GUI,'distanceField').min(20).max(100).step(5).name('Distance Field').onChange(function(value){landscape.reset()});
pattern.add(GUI,'xFactor').min(0.01).max(0.18).step(0.01).name('X-factor').onChange(function(value){landscape.reset()});
pattern.add(GUI,'yFactor').min(0.01).max(0.04).step(0.01).name('Y-factor').onChange(function(value){landscape.reset()});
pattern.open();
window.addEventListener('resize', function() { window.cancelAnimationFrame(landscape.timer); landscape.reset(); landscape.draw();
});
canvas.addEventListener('click', function() { window.cancelAnimationFrame(landscape.timer); landscape.reset(); landscape.draw();
});
landscape.init();
landscape.draw();
Landscape - Script Codes
Landscape - Script Codes
Home Page Home
Developer Endre Simo
Username esimov
Uploaded November 28, 2022
Rating 4
Size 3,792 Kb
Views 24,288
Do you need developer help for Landscape?

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!

Endre Simo (esimov) 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!