See Gamedev in JS
One...
Two...
Three words
about me.
That's me
Student of Taras Shevchenko National University of Kyiv
Faculty: cybernetics
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5217366/IMG_7036.jpg)
#100DaysOfCode
About myself:
A Girl with great ambitions and low budget
Where you can find me
Let's imagine the situation...
Web developer
![](https://images.vexels.com/media/users/3/127483/isolated/preview/932961439f83a1a480dd0fea60b2d71b-comic-speech-bubble-doodle-by-vexels.png)
I don't want to be a web developer
![](https://images.vexels.com/media/users/3/127483/isolated/preview/932961439f83a1a480dd0fea60b2d71b-comic-speech-bubble-doodle-by-vexels.png)
I want to be...
a game developer
![](https://www.meme-arsenal.com/memes/d19d258b26798acef75041e24ba5c285.jpg)
But before you start
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5251204/Screen_Shot_2018-09-11_at_9.02.51_PM.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5251205/Screen_Shot_2018-09-11_at_9.03.25_PM.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5251203/Screen_Shot_2018-09-11_at_9.02.44_PM.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5251202/Screen_Shot_2018-09-11_at_9.02.40_PM.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5251201/Screen_Shot_2018-09-11_at_9.02.28_PM.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5251200/Screen_Shot_2018-09-11_at_9.02.21_PM.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5251199/Screen_Shot_2018-09-11_at_9.02.16_PM.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5251198/Screen_Shot_2018-09-11_at_8.59.57_PM.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5251197/Screen_Shot_2018-09-11_at_9.02.08_PM.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5251196/Screen_Shot_2018-09-11_at_8.57.27_PM.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5251195/Screen_Shot_2018-09-11_at_8.58.00_PM.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5251194/Screen_Shot_2018-09-11_at_8.56.09_PM.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5251193/Screen_Shot_2018-09-11_at_8.55.56_PM.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5251893/Screen_Shot_2018-09-11_at_10.22.41_PM.png)
Put your
devhats on
Let's the party begin
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5217441/pasted-from-clipboard.png)
Chapter 1
Make a plan
Game
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5251345/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5251345/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5251345/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5251345/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5251345/pasted-from-clipboard.png)
Speed
Physics
Sound
Animation
Platforms
Chapter 2
Let's choose language
Common choice
-
C++
-
Java
-
Swift
-
C#
-
HTML5
-
Unreal Engine
-
Unity
-
id Tech 4
-
Source
![](https://media0.giphy.com/media/d3mlE7uhX8KFgEmY/giphy.gif)
Why HTML5
Java Applets
Flash apps
Silverlight apps
Unity Web Player
<= require plugin
<= require plugin
<= require plugin
<= require plugin
![](https://media3.giphy.com/media/d2Z32wTIuPl6iHuw/giphy.gif)
HTML5
<= no plugins, just browser!
Why to bother at all?
- build once and deploy everywhere
- no installs, no plugins, just instant gaming right in the browser
- millions of potential users/clients
- millions of potential developers
- huge database of ready-to-go solutions, libraries, engines and frameworks
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5251394/pasted-from-clipboard.png)
So where are those games?
-
mobile games
-
games markets & promo sites
-
Steam
-
Playable ads
Duelyst
![](https://vignette.wikia.nocookie.net/duelyst/images/c/c4/6ec586a775f1326b09b066cdc24f91c4_large.gif/revision/latest?cb=20140409204304)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5225331/pasted-from-clipboard.png)
Heroes of Paragon
Kopanito All-Stars Soccer
Let's have a look
http://10.0.0.238:3000
To do list
-
Language -
Physic
-
Sound
-
Animation
-
Speed
Chapter 3
Let's create a game
HTML5
JS
CSS
DOM
var getRandomNumber = function (size) {
return Math.floor(Math.random() * size);
};
//calculate distance between click(event) to treasure (target)
var getDistance = function (event, target) {
var diffX = event.offsetX - target.x;
var diffY = event.offsetY - target.y;
return Math.sqrt((diffX * diffX) + (diffY * diffY));
};
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5251498/pasted-from-clipboard.png)
Canvas
function startGame() {
myGamePiece = new component(30, 30, "red", 10, 120);
myGamePiece.gravity = 0.05;
myScore = new component("30px", "Consolas", "black", 280, 40, "text");
myGameArea.start();
}
var myGameArea = {
canvas : document.createElement("canvas"),
start : function() {
this.canvas.width = 480;
this.canvas.height = 270;
this.context = this.canvas.getContext("2d");
document.body.insertBefore(this.canvas, document.body.childNodes[0]);
this.frameNo = 0;
},
clear : function() {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
}
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5251507/Screen_Shot_2018-09-11_at_10.08.07_PM.png)
But...
It's rather boring...
We need some tools!!!
![](https://media1.giphy.com/media/NgVDOJ4yVyrwk/giphy.gif)
![](https://media3.giphy.com/media/AEdycxjsA0dcYzFsyv/giphy.gif)
![](https://media0.giphy.com/media/6Wct6ExFm8NwY/giphy.gif)
![](https://media3.giphy.com/media/4KSG2wcubH6yk/giphy.gif)
![](https://media0.giphy.com/media/l2Je5V9zP772cw4Ss/giphy.gif)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5218910/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5218912/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5218936/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5218944/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5218947/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5218955/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5218958/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5218963/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5218966/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5218971/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5218976/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5218981/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5218998/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5219002/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5219042/pasted-from-clipboard.png)
Wow...Slow down
![](https://media3.giphy.com/media/9SIqlPusz8iLh1IFFJ/giphy.gif)
Chapter 4
Frameworks
Physic
Goal:
-
Simple collision detection
-
Simple manifold generation
-
Impulse resolution
- etc.
How to reach success?
Solution
-
Write your own code with the help of formulas
-
Use frameworks
-
Use libraries
Frameworks
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5218910/pasted-from-clipboard.png)
-
Arcade Physics
-
P2
-
Ninja Physics
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5251576/pasted-from-clipboard.png)
Example:
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5251650/pasted-from-clipboard.png)
is a 2D physics engine for the web
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5251673/Screen_Shot_2018-09-11_at_10.53.12_PM.png)
A lightweight and simple 3D physics engine for the web.
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5251647/Screen_Shot_2018-09-11_at_10.45.33_PM.png)
Look an example
Load the physics shapes and sprites
function preload() {
// Load sprite sheet generated with TexturePacker
this.load.atlas('sheet', 'assets/fruit-sprites.png', 'assets/fruit-sprites.json');
// Load body shapes from JSON file
//generated using PhysicsEditor
this.load.json('shapes', 'assets/fruit-shapes.json');
}
Load magic line for physic
this.matter.world.setBounds(0, 0,
game.config.width, game.config.height);
// add some objects
this.matter.add.sprite(200, 50, 'sheet', 'crate',
{shape: shapes.crate});
this.matter.add.sprite(250, 250, 'sheet', 'banana',
{shape: shapes.banana});
this.matter.add.sprite(360, 50, 'sheet', 'orange',
{shape: shapes.orange});
this.matter.add.sprite(400, 250, 'sheet', 'cherries',
{shape: shapes.cherries});
Now add some more objects in the scene:
By click add more bananas
this.input.on('pointerdown', function (pointer) {
this.matter.add.sprite(pointer.x, pointer.y,
'sheet', 'banana', {shape: shapes.banana});
}, this);
}
![](https://media0.giphy.com/media/11sBLVxNs7v6WA/giphy.gif)
-
Language -
Physic -
Sound
-
Animation
-
Speed
To do List
Chapter 5
Frameworks
Sounds
Difficulties
-
Autoplay
-
Volume
-
Concurrent audio playback
-
Testing and support
Ways to work around the issues
-
Audio sprites
-
HTMLMediaElement
-
XMLHttpRequest
-
WebAudio
-
Libraries
Library
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5219002/pasted-from-clipboard.png)
- Zero Dependencies
- Audio Sprites
- Works Everywhere
Most basic
var sound = new Howl({
src: ['sound.mp3']
});
sound.play();
Define and play a sound sprite:
var sound = new Howl({
src: ['sounds.webm', 'sounds.mp3'],
sprite: {
blast: [0, 3000],
laser: [4000, 1000],
winner: [6000, 5000]
}
});
// Shoot the laser!
sound.play('laser');
-
Language -
Physic -
Sound -
Animation
-
Speed
To do List
Chapter 6
Frameworks
Animation
Considerations
-
CSS transform
-
requestAnimationFrame
-
A take-away
var startTime = -1;
var animationLength = 2000; // Animation length in milliseconds
function doAnimation(timestamp) {
// Calculate animation progress
var progress = 0;
if (startTime < 0) {
startTime = timestamp;
} else {
progress = timestamp - startTime;
}
// Do animation ...
if (progress < animationLength) {
requestAnimationFrame(doAnimation);
}
}
// Start animation
requestAnimationFrame(doAnimation);
Libraries
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5218912/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5251628/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5218976/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5219042/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5218910/pasted-from-clipboard.png)
Chapter 7
Brief look
PixiJS
Create great interactive graphics for Games and Web
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5218912/pasted-from-clipboard.png)
What isn't PiXi
A game engine
No built-in :
- keyboard input
- collision detection
- sound engine
- animation engine Particles
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5218912/pasted-from-clipboard.png)
![](https://media2.giphy.com/media/26xBD8jb96cfiUEbS/giphy.gif)
Then...What is Pixi?
- Wrapper for WebGL
- Multimedia engine
- Interactive
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5218912/pasted-from-clipboard.png)
What PiXi gives us
- Sprites
- Easy loading
- Flexible data structure
- Cross-device compatibility
- JavaScript!
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5218912/pasted-from-clipboard.png)
Chapter 8
Usage of PiXiJS
Setting up
$> npm install pixi.js
/** CDN Install (via cdnjs) **/
<script src="https://cdnjs.cloudflare.com/ajax/libs/pixi.js/4.5.1/pixi.min.js"></script>
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5223904/pasted-from-clipboard.png)
Create background
var PIXI = require('pixi.js');
var app = new PIXI.Application(800, 600, {backgroundColor : 0x6889ff});
document.body.appendChild(app.view);
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5223922/pasted-from-clipboard.png)
Result
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5223926/pasted-from-clipboard.png)
Create Sprite Animation
let roleImages = ["images/mario1.png","images/mario2.png","images/mario3.png","images/mario4.png"];
let textureArray = [];
roleImages.forEach((role) => {
let texture = PIXI.Texture.fromImage(role);
textureArray.push(texture);
});
let mario = new PIXI.extras.AnimatedSprite(textureArray);
mario.animationSpeed = 0.1;
mario.play();
mario.interactive = true;
mario.position = new PIXI.Point(0, 538);
app.stage.addChild(mario);
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5223934/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5223935/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5223937/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5223939/pasted-from-clipboard.png)
Result
![](https://s3.amazonaws.com/media-p.slid.es/uploads/812420/images/4523142/mario_run.gif)
Layer
let tilingSprite =
new PIXI.extras.TilingSprite.fromImage(
'images/ground.png',
app.screen.width,
app.screen.height
);
tilingSprite.y = app.screen.height - 32;
tilingSprite.scale = new PIXI.Point(2, 2);
app.stage.addChild(tilingSprite);
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5223980/pasted-from-clipboard.png)
Result
![](https://s3.amazonaws.com/media-p.slid.es/uploads/812420/images/4523168/mario_run_2.gif)
-
Language -
Physic -
Sound -
Animation -
Speed
To do List
Chapter 9
Future of HTML5
Games need performance!
JS is sloooow !
How about some speedup?
![](https://media3.giphy.com/media/3o7qE8co3YAzNUoAW4/giphy.gif)
asm.js
- speed race since V8 by Google
- JS: dynamic, untyped, and interpreted
JIT - Just In Time Compiler
- compilation at execution
- adaptive optimization
- asm.js - strict subset of JS, generated by
source to source compilers
Result?
Up to 66% of native speed
![](https://media2.giphy.com/media/d0NnEG1WnnXqg/giphy.gif)
On a browser!!!
Who uses it?
- as a proof of
concept Mozilla portedsimple
Unreal Engine game (asm .js+WebGL interface)
- some heavy-performance JS libs use it
- game engines like Unity, Godot or Unreal allow
to export game to HTML5
Is that all?
Nope
Web Assembly is on the way
= native browser sandbox to run bytecode
![](https://media2.giphy.com/media/qAB5xbv44cCAg/giphy.gif)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5261408/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5261409/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5261411/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5261413/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5261417/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5261420/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5261424/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5261426/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5261432/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5261442/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/920266/images/5261564/pasted-from-clipboard.png)
-
Parsing — the time it takes to process the source code into something that the interpreter can run.
-
WebAssembly doesn’t need to go through this transformation because it is already a bytecode.
-
Compiling + optimizing — the time that is spent in the baseline compiler and optimizing compiler. Some of the optimizing compiler’s work is not on the main thread, so it is not included here.
-
The compiler doesn’t have to spend time running the code to observe what types are being used before it starts compiling optimized code.
-
More optimizations have already been done ahead of time in LLVM
-
Re-optimizing — the time the JIT spends readjusting when its assumptions have failed, both re-optimizing code and bailing out of optimized code back to the baseline code.
-
In WebAssembly, things like types are explicit, so the JIT doesn’t need to make assumptions about types based on data it gathers during runtime.
-
Execution — the time it takes to run the code.
-
Many of the optimizations that JITs make to JavaScript just aren’t necessary with WebAssembly.
-
Garbage collection — the time spent cleaning up memory.
-
For now, WebAssembly does not support garbage collection at all. Memory is managed manually
Chapter 10
Final
Creating games is not so hard as it seems
![](https://media1.giphy.com/media/YFis3URdQJ6qA/giphy.gif)
Results??!
![](https://media3.giphy.com/media/RhPvGbWK78A0/giphy.gif)
Give some little magic
![](https://media3.giphy.com/media/12NUbkX6p4xOO4/giphy.gif)
![](https://media1.giphy.com/media/UDyym2MWwItG0/giphy.gif)
Thank you for attention
Gamedev in JS
By Khrystyna Landvytovych
Gamedev in JS
- 971