• Stars
    star
    212
  • Rank 186,122 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated almost 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A function to scale an HTML canvas element to the maximum browser window

Scale and align an HTML element in the browser

Use the function scaleToWindow to scale an HTML element to the maximum size of the browser's window. scaleToWindow will also align the element for the best vertical or horizontal fit inside the browser window. For example, if you have an element that's wider than it is tall, it will be centered vertically inside the browser. If the element is taller than it is wide, it will be centered horizontally.

Alignment

Here's how to use scaleToWindow:

scaleToWindow(anyElement, borderColor);

(If you are using Pixi, supply the renderer.view as the element.) The optional second argument lets you set the color of the browser's background that borders the element. You can supply any RGB, HSLA or Hexadecimal color value, as well as the any HTML color string, like β€œblue” or β€œred”. (If you don't supply this optional color, the border will be set to a neutral dark gray: #2C3539.)

The scaleToWindow function also returns the scale value that the element is scaled to. You can find it like this:

var scale = scaleToWindow(renderer.view);

This will give you a number, like 1.98046875, which tells you the ratio by which the element was scaled. This might be an important value to know if you need to convert browser pixel coordinates to the scaled pixel values of the element. For example, if you have a pointer object which tracks the mouse's position in the browser, you might need to convert those pixel positions to the scaled element coordinates to find out if the mouse is touching something in the element. Some general code like this will do the trick:

pointer.x = pointer.x / scale;
pointer.y = pointer.y / scale;

Optionally, you might also want the element to rescale itself every time the size of the browser window is changed. If that’s the case, call scaleToWindow inside a window event listener:

window.addEventListener("resize", function(event){ 
  scaleToWindow(anyelementElement);
});

For the best effect, make sure that you set the browser's default margins and padding to 0 on all HTML elements. If you don't do this, most browsers will add some padding around the element borders. This bit of CSS will do the trick:

<style>* {padding: 0; margin: 0}</style>

If you prefer, you can add this CSS style using JavaScript in your main program like this:

var newStyle = document.createElement("style");
var style = "* {padding: 0; margin: 0}";
newStyle.appendChild(document.createTextNode(style));
document.head.appendChild(newStyle);

More Repositories

1

learningPixi

A step-by-step introduction to making games and interactive media with the Pixi.js rendering engine.
4,387
star
2

hexi

Make games the fun way!
JavaScript
551
star
3

ga

The world's tiniest, cutest and funnest game engine
JavaScript
451
star
4

sound.js

A micro-library to load, play and generate sound effects and music for games and interactive applications
JavaScript
299
star
5

bump

A set of 2D collision utilities for games.
JavaScript
279
star
6

tink

A set of sprite interactivity tools for Pixi
JavaScript
121
star
7

charm

JavaScript
118
star
8

smoothie

Ultra-smooth sprite animation for Pixi using true delta-time interpolation
JavaScript
98
star
9

dust

JavaScript
91
star
10

spriteUtilities

Useful functions for creating and working with sprites in Pixi
JavaScript
86
star
11

agd

Source code for Advanced Game Design with HTML5 and JavaScript
JavaScript
46
star
12

tileUtilities

Utilities for make tile based-games for the Pixi renderer
JavaScript
32
star
13

learnPixiJS

Source code repository for the book "Learn PixiJS"
JavaScript
24
star
14

gameUtilities

JavaScript
22
star
15

thepoetrybook

An easy way to make a poetry book or static website using a single markdown document
JavaScript
8
star
16

agdt

Source code repository for the book "The Advanced Game Developers Toolkit"
JavaScript
8
star
17

learningGit

A beginner's guide to using Git
8
star
18

keyboard

A simple and easy-to-use function called `keyboard` that listens for and captures keyboard events
JavaScript
6
star
19

dwc2014

Repo for Dev Workshop Conference 2014
5
star
20

navigationMenuBar

A basic navigation menu bar written in Elm
JavaScript
4
star
21

propertyObserver

JavaScript
3
star
22

helloElm

A very simple introduction the Elm programming language
2
star
23

fullScreen

JavaScript
2
star
24

contain

Keep a sprite contained inside an area
JavaScript
2
star
25

hitTestRectangle

A function that checks for a collision between two rectangular shaped sprites.
JavaScript
2
star
26

basicNavigationBar

JavaScript
2
star
27

ohwell

A jumping game for children.
JavaScript
1
star
28

kittykatattack

Website
JavaScript
1
star
29

randomNumberGame

A simple number guessing game using random numbers, written in Elm
HTML
1
star
30

ecs

Simple JavaScript ECS
1
star
31

storymaker

storyMaker.js is a quick way to create multiple stories from a single story template. You can use this in an interetive, game, or for lots of other fun things.
JavaScript
1
star