Code Monkey home page Code Monkey logo

canvas-autoscale's Introduction

canvas-autoscale deprecated

Deprecated: this doesn't work as well as I'd like it to, and would be better off rewritten :)

A variant of canvas-fit that handles some extra magic for you: adjusting the scale of the canvas to maintain smooth framerates.

As of version 2.x.x, the approach for scaling has changed slightly to make it more predictable.

Usage

NPM

resize = autoscale(canvas, [options], [updated])

Returns a resize function you can use to update the size of the canvas to fit within its parent element. Takes the following options:

  • parent: the parent element to fit within. Defaults to the canvas' parent element.
  • scales: an array of scales to attempt. Defaults to [0.25, 0.5, 1, devicePixelRatio].
  • target: a two-element array containing the target framerates โ€“ the first element is the maximum rate before reducing scale, and the second is the minimum before increasing it again. Defaults to [55, 59].
  • fps: a custom function that should return the current framerate. Optional.
  • gap: the amount of frames to wait between rescales. Defaults to 60.
  • auto: automatically resize/rescale the canvas in response to window resizes and framerate changes respectively. Defaults to true, set to false to disable.
  • sensitivity: the amount of sensitivity for canvas rescales. Set to 0 to ignore rescales, and 1 to make them immediate. It's recommended you choose a figure around 0.01, which is the default.

updated is called every time the canvas size is changed โ€“ pass your render function in here to avoid the screen flickering every time your canvas is resized.

resize.tick()

If you're using auto: false, call this method once per frame to check the canvas' framerate and update the scale accordingly.

resize.scale

Read-only property to get the current scale of the canvas.

resize.rate

The most recent framerate captured.

resize()

Manually trigger a resize of the canvas. Useful, for example, when resizing the screen.

License

MIT. See LICENSE.md for details.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.