Simple HTML Canvas with Arrow Key Controls
How do I make an simple html canvas with arrow key controls?
What is a simple html canvas with arrow key controls? How do you make a simple html canvas with arrow key controls? This script and codes were developed by Andy Hoffman on 24 August 2022, Wednesday.
Simple HTML Canvas with Arrow Key Controls - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Simple HTML Canvas with Arrow Key Controls</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <canvas id="canvas"></canvas> <script src="js/index.js"></script>
</body>
</html>
Simple HTML Canvas with Arrow Key Controls - Script Codes CSS Codes
canvas { border: 1px solid black; position: absolute; top: 50%; left: 50%; width: 300px; height: 200px; transform: translateY(-50%) translateX(-50%);
}
Simple HTML Canvas with Arrow Key Controls - Script Codes JS Codes
var canvas;
var ctx;
var dx = 5;
var dy = 5;
var x = 150;
var y = 100;
var WIDTH = 300;
var HEIGHT = 200;
var SMALL_RECT_WIDTH = 50;
var SMALL_RECT_HEIGHT = 20;
function init() { canvas = document.getElementById('canvas'); ctx = canvas.getContext('2d'); draw();
}
function rect(x,y,w,h) { ctx.beginPath(); ctx.rect(x,y,w,h); ctx.closePath(); ctx.fill(); ctx.stroke();
}
function clear() { ctx.clearRect(0, 0, WIDTH, HEIGHT);
}
function keydownHandler(evt) { switch (evt.keyCode) { case 82: // 'r' ctx.style.transform = 'rotate(360deg)'; break; case 38: /* Up arrow was pressed */ y -= dy; break; case 40: /* Down arrow was pressed */ y += dy; break; case 37: /* Left arrow was pressed */ x -= dx; break; case 39: /* Right arrow was pressed */ x += dx; break; }
}
function draw() { clear(); ctx.strokeStyle = 'black'; ctx.fillStyle = 'purple'; rect(x-(SMALL_RECT_WIDTH *.5), y-(SMALL_RECT_HEIGHT + 15), SMALL_RECT_WIDTH, SMALL_RECT_HEIGHT); requestAnimationFrame(draw);
}
init();
window.addEventListener('keydown', keydownHandler, true); // Cross-browser support for requestAnimationFrame
window.requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame || window.mozRequestAnimationFrame;
Developer | Andy Hoffman |
Username | antibland |
Uploaded | August 24, 2022 |
Rating | 3 |
Size | 2,144 Kb |
Views | 32,384 |
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 |
Here today | 3,201 Kb |
Full page image fade in | 1,849 Kb |
Trusted Clicks | 3,314 Kb |
Pure CSS Flexbox Navigation | 3,012 Kb |
Animated Stacked Pages | 9,137 Kb |
SVG With Pure CSS Tooltip | 2,002 Kb |
Working with jQuery Marquee plugin | 2,126 Kb |
Flexbox 3-Column Layout | 1,901 Kb |
Flexbox Wrapping | 1,720 Kb |
Accessible Tooltip | 2,912 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 |
Flat UI Button | Honchoman | 2,289 Kb |
Automatic scroll | Skeurentjes | 4,042 Kb |
Weather App | OmranAbazid | 2,596 Kb |
Countdown Timer | Massiebn | 3,001 Kb |
Hovers with popups | Zacharyolson | 2,380 Kb |
Expert Help | SinceSidSlid | 4,064 Kb |
Animated Donut Chart | Jplhomer | 3,808 Kb |
Growing Root - Scroll control - CANVAS | Cjonasw | 2,342 Kb |
SnappySnippet Test | Elmsoftware | 8,385 Kb |
Blog Concept 2 | JGallardo | 2,994 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!