diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b9e4aba --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules/ +app.js diff --git a/LICENSE b/LICENSE index 2071b23..f5c8341 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) +Copyright (c) 2021 Alexander Popov 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: diff --git a/index.html b/index.html new file mode 100644 index 0000000..7e18a21 --- /dev/null +++ b/index.html @@ -0,0 +1,14 @@ + + + + + jsKeyPressed + + + + Canvas not available. + + + + + diff --git a/js/index.js b/js/index.js new file mode 100644 index 0000000..8267c69 --- /dev/null +++ b/js/index.js @@ -0,0 +1,67 @@ +let canvas = null; +let context = null; +let cW = null; +let cH = null; + +let pressedKeys = {}; + +window.onload = function() { + 'use strict'; + + canvas = document.getElementById('app'); + context = canvas.getContext('2d'); + cW = canvas.width; + cH = canvas.height; + + window.requestAnimationFrame(GameLoop); +} + +window.onkeyup = function(e) { pressedKeys[e.code] = false; } +window.onkeydown = function(e) { pressedKeys[e.code] = true; } + +function GameLoop(timeStamp) { + 'use strict'; + + Update(); + Draw(); + + window.requestAnimationFrame(GameLoop); +} + +function Draw() { + 'use strict'; + + // clean context + context.fillStyle = '#999999'; + context.fillRect(0, 0, cW, cH); + + context.fillStyle = '#ffffff'; + if(pressedKeys['KeyW']) { + context.fillStyle = '#ff0000'; + } + context.fillRect(cW / 2 - 25, 100, 50, 50); // W + + context.fillStyle = '#ffffff'; + if(pressedKeys['KeyA']) { + context.fillStyle = '#ff0000'; + } + context.fillRect(cW / 2 - 75, 150, 50, 50); // A + + context.fillStyle = '#ffffff'; + if(pressedKeys['KeyS']) { + context.fillStyle = '#ff0000'; + } + context.fillRect(cW / 2 - 25, 150, 50, 50); // S + + context.fillStyle = '#ffffff'; + if(pressedKeys['KeyD']) { + context.fillStyle = '#ff0000'; + } + context.fillRect(cW / 2 + 25, 150, 50, 50); // D +} + +function Update() { + 'use strict'; + + // +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..4c8d409 --- /dev/null +++ b/package.json @@ -0,0 +1,22 @@ +{ + "name": "jskeypressed", + "version": "1.0.0", + "description": "", + "main": "./js/index.js", + "scripts": { + "start": "webpack serve", + "build": "webpack --mode=production", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "gitea@iiiypuk.me:iiiypuk/jsKeyPressed.git" + }, + "author": "Alexander Popov", + "license": "MIT", + "devDependencies": { + "webpack": "^5.45.1", + "webpack-cli": "^4.7.2", + "webpack-dev-server": "^3.11.2" + } +} diff --git a/webpack.config.js b/webpack.config.js new file mode 100644 index 0000000..954e23f --- /dev/null +++ b/webpack.config.js @@ -0,0 +1,15 @@ +const path = require('path'); + +module.exports = { + mode: 'development', + entry: './js/index.js', + output: { + path: path.resolve(__dirname, './'), + filename: 'app.js', + }, + devServer: { + contentBase: path.join(__dirname, './'), + compress: false, + port: 55555, + }, +};