Canvas Parallax Skyline
How do I make an canvas parallax skyline?
This pen was inspired by Random Skyline by moklick. Move your mouse to change speed and move up and down. It would be cool to implement this as a background for a game. I am utilizing Sketch.js in this pen.. What is a canvas parallax skyline? How do you make a canvas parallax skyline? This script and codes were developed by Jack Rugile on 11 August 2022, Thursday.
Canvas Parallax Skyline - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Canvas Parallax Skyline</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css"> <style> /* NOTE: The styles were added inline because Prefixfree needs access to your styles and they must be inlined if they are on local disk! */ canvas { background: linear-gradient( hsl(200, 50%, 80%) 0%, hsl(200, 30%, 95%) 75%) ; display: block;
}
div { height: 100%; left: 0; position: fixed; top: 0; width: 100%; background: url(http://jackrugile.com/images/misc/skyline-texture.png);
} </style> <script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
</head>
<body> <div></div>
<!--
Canvas Parallax Skyline
-----------------------
Move your mouse to change speed and move up and down.
--> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src='https://rawgithub.com/soulwire/sketch.js/master/js/sketch.min.js'></script> <script src="js/index.js"></script>
</body>
</html>
Canvas Parallax Skyline - Script Codes CSS Codes
canvas { background: linear-gradient( hsl(200, 50%, 80%) 0%, hsl(200, 30%, 95%) 75%) ; display: block;
}
div { height: 100%; left: 0; position: fixed; top: 0; width: 100%; background: url(http://jackrugile.com/images/misc/skyline-texture.png);
}
Canvas Parallax Skyline - Script Codes JS Codes
(function() { var Building, Skyline, dt, sketch, skylines; sketch = Sketch.create(); sketch.mouse.x = sketch.width / 10; sketch.mouse.y = sketch.height; skylines = []; dt = 1; Building = function(config) { return this.reset(config); }; Building.prototype.reset = function(config) { this.layer = config.layer; this.x = config.x; this.y = config.y; this.width = config.width; this.height = config.height; this.color = config.color; this.slantedTop = floor(random(0, 10)) === 0; this.slantedTopHeight = this.width / random(2, 4); this.slantedTopDirection = round(random(0, 1)) === 0; this.spireTop = floor(random(0, 15)) === 0; this.spireTopWidth = random(this.width * .01, this.width * .07); this.spireTopHeight = random(10, 20); this.antennaTop = !this.spireTop && floor(random(0, 10)) === 0; this.antennaTopWidth = this.layer / 2; return this.antennaTopHeight = random(5, 20); }; Building.prototype.render = function() { sketch.fillStyle = sketch.strokeStyle = this.color; sketch.lineWidth = 2; sketch.beginPath(); sketch.rect(this.x, this.y, this.width, this.height); sketch.fill(); sketch.stroke(); if (this.slantedTop) { sketch.beginPath(); sketch.moveTo(this.x, this.y); sketch.lineTo(this.x + this.width, this.y); if (this.slantedTopDirection) { sketch.lineTo(this.x + this.width, this.y - this.slantedTopHeight); } else { sketch.lineTo(this.x, this.y - this.slantedTopHeight); } sketch.closePath(); sketch.fill(); sketch.stroke(); } if (this.spireTop) { sketch.beginPath(); sketch.moveTo(this.x + (this.width / 2), this.y - this.spireTopHeight); sketch.lineTo(this.x + (this.width / 2) + this.spireTopWidth, this.y); sketch.lineTo(this.x + (this.width / 2) - this.spireTopWidth, this.y); sketch.closePath(); sketch.fill(); sketch.stroke(); } if (this.antennaTop) { sketch.beginPath(); sketch.moveTo(this.x + (this.width / 2), this.y - this.antennaTopHeight); sketch.lineTo(this.x + (this.width / 2), this.y); sketch.lineWidth = this.antennaTopWidth; return sketch.stroke(); } }; Skyline = function(config) { this.x = 0; this.buildings = []; this.layer = config.layer; this.width = { min: config.width.min, max: config.width.max }; this.height = { min: config.height.min, max: config.height.max }; this.speed = config.speed; this.color = config.color; this.populate(); return this; }; Skyline.prototype.populate = function() { var newHeight, newWidth, results, totalWidth; totalWidth = 0; results = []; while (totalWidth <= sketch.width + (this.width.max * 2)) { newWidth = round(random(this.width.min, this.width.max)); newHeight = round(random(this.height.min, this.height.max)); this.buildings.push(new Building({ layer: this.layer, x: this.buildings.length === 0 ? 0 : this.buildings[this.buildings.length - 1].x + this.buildings[this.buildings.length - 1].width, y: sketch.height - newHeight, width: newWidth, height: newHeight, color: this.color })); results.push(totalWidth += newWidth); } return results; }; Skyline.prototype.update = function() { var firstBuilding, lastBuilding, newHeight, newWidth; this.x -= (sketch.mouse.x * this.speed) * dt; firstBuilding = this.buildings[0]; if (firstBuilding.width + firstBuilding.x + this.x < 0) { newWidth = round(random(this.width.min, this.width.max)); newHeight = round(random(this.height.min, this.height.max)); lastBuilding = this.buildings[this.buildings.length - 1]; firstBuilding.reset({ layer: this.layer, x: lastBuilding.x + lastBuilding.width, y: sketch.height - newHeight, width: newWidth, height: newHeight, color: this.color }); return this.buildings.push(this.buildings.shift()); } }; Skyline.prototype.render = function() { var i; i = this.buildings.length; sketch.save(); sketch.translate(this.x, (sketch.height - sketch.mouse.y) / 20 * this.layer); while (i--) { this.buildings[i].render(i); } return sketch.restore(); }; sketch.setup = function() { var i, results; i = 5; results = []; while (i--) { results.push(skylines.push(new Skyline({ layer: i + 1, width: { min: (i + 1) * 30, max: (i + 1) * 40 }, height: { min: 150 - (i * 35), max: 300 - (i * 35) }, speed: (i + 1) * .003, color: 'hsl( 200, ' + (((i + 1) * 1) + 10) + '%, ' + (75 - (i * 13)) + '% )' }))); } return results; }; sketch.clear = function() { return sketch.clearRect(0, 0, sketch.width, sketch.height); }; sketch.update = function() { var i, results; dt = sketch.dt < .1 ? .1 : sketch.dt / 16; dt = dt > 5 ? 5 : dt; i = skylines.length; results = []; while (i--) { results.push(skylines[i].update(i)); } return results; }; sketch.draw = function() { var i, results; i = skylines.length; results = []; while (i--) { results.push(skylines[i].render(i)); } return results; }; $(window).on('mousemove', function(e) { sketch.mouse.x = e.pageX; return sketch.mouse.y = e.pageY; });
}).call(this);
Developer | Jack Rugile |
Username | jackrugile |
Uploaded | August 11, 2022 |
Rating | 4.5 |
Size | 4,871 Kb |
Views | 42,504 |
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!
Name | Size |
Canvas Parallax Mountains | 4,573 Kb |
Rainbow Firestorm | 3,052 Kb |
Canvas Wave | 2,902 Kb |
Canvas Orbital Trails v2 | 3,421 Kb |
SpacePi js13k | 19,847 Kb |
Canvas Sparkly Circle Loader | 2,446 Kb |
CSS3 Pseudo Sound Bars | 2,400 Kb |
Light Loader | 2,738 Kb |
Develop Denver Logo | 3,813 Kb |
Canvas Orbital Trails | 3,697 Kb |
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!
Name | Username | Size |
Twitch TV | Natester13 | 4,488 Kb |
Animated Logo | Shakdaniel | 2,672 Kb |
Slide In Panel | Vikvarg | 2,811 Kb |
Kut D3 | Jellevrswk | 3,687 Kb |
SCSS Simple Animated Drop-In | Danwarfel | 2,175 Kb |
Voting App - register | MatheusLima92 | 1,948 Kb |
DevCamp 2014 - Denver Public Library | See8ch | 5,033 Kb |
Super Discount | Orrinward | 3,225 Kb |
Pure CSS Menu | Bronsrobin | 3,321 Kb |
Pomodoro Clock | Yas46 | 3,328 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!