Giter Site home page Giter Site logo

codepo8 / canvasshift.js Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 1.0 37 KB

A helper function to shift images on a canvas to allow for image tiling and conversion.

Home Page: https://codepo8.github.io/canvasshift.js/shift-test.html

HTML 86.83% JavaScript 13.17%
shifting bitmaps images canvas javascript

canvasshift.js's Introduction

CanvasShift is a helper function that allows you to shift images on a canvas.

This means you can rotate the images in themselves and users can save the results as images, something CSS sadly enough can't do.

The easiest way to test this is to check the demo pages.

Here's a bare bones example:

<script src="canvasshift.js"></script>
<script>
    let c = document.createElement('canvas');
    let cx = c.getContext('2d');
    document.body.appendChild(c);
    let i = new Image();
    i.src = 'puking-unicorn.jpg';
    i.onload = function() {
      c.height = i.naturalHeight;
      c.width = i.naturalWidth;
      cx.drawImage(i, 0, 0);
      canvasshift(c, 100, 0);
    }
</script>

If you don't want to do the loading of the image yourself, you can also use the load() method of canvasshift.js:

let c = document.createElement('canvas');
let button = document.querySelector('button');
button.appendChild(c);

const shift = _ => {
  canvasshift(c, -200);
}
canvasshift.load(c, 'puking-unicorn.jpg', shift);

button.addEventListener('click', e => {
  canvasshift(c, 100, 0);
  e.preventDefault();
});

More details are in this blog post.

canvasshift.js's People

Contributors

codepo8 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

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.