1.0.0 • Published 5 months ago

sequence-canvas v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

sequence-canvas

Frame-by-frame reproduction of images in HTML Canvas

https://jsfiddle.net/ehoop1337/ukg3L75y/

Installation

Package Manager

    npm install sequence-canvas

ES Module

    import SequenceCanvas from 'sequence-canvas';
    
    const canvas = new SequenceCanvas({
      // settings
    });

CommonJS

    const SequenceCanvas = require('sequence-canvas');
    
    const canvas = new SequenceCanvas({
      // settings
    });

Script

HTML

    <script src="sequence-canvas.js"></script>
    <script>
        const canvas = new SequenceCanvas({
            // settings
        });
    </script>

CDN

https://cdn.jsdelivr.net/gh/ehoop1337/sequence-canvas/lib/sequence-canvas.js

Example

const canvas = new SequenceCanvas({
  // required object
  canvas: {
    // required property
    element: document.querySelector('canvas'),
    width: innerWidth,
    height: innerHeight,
  },
  // required object
  images: {
    // required property
    paths: ['../img/01.webp', '../img/02.webp', '../img/03.webp'],
    options: {
      size: {
        width: 400,
        height: 400,
      },
      position: {
        x: innerWidth / 2 - 200,
        y: innerHeight / 2 - 200
      }
    }
  },
  init: true,
  direction: -1,
  loop: false,
  fps: 30,
  startIndex: 0,
  finishIndex: 2,
  currentIndex: 0,
  startImmediately: false,
  startAfterLoaded: true,
  logging: true,
  on: {
    init: function () {
      console.log('init');
    }
  }
});

canvas.on('stop', function() { console.log('stop')});

Settings

Required

OptionTypeDescription
canvasobjectAn object that has an element and images property.
canvas.elementHTMLCanvasElementNeed to pass HTMLCanvasElement.
imagesobjectAn object that has the paths and options properties.
images.pathsstring[]An array of strings with paths to images.

Optional

OptionTypeDefaultDescription
canvas.widthnumbercanvas.element.widthWidth of the canvas.
canvas.heightnumbercanvas.element.heightHeight of the canvas
images.optionsobjectAn object containing images settings.
images.options.positionobjectIt can be used without specifying the trim and size properties.
images.options.position.xnumberThe X coordinate on the canvas where the image will be placed.
images.options.position.ynumberThe Y coordinate on the canvas where the image will be placed.
images.options.trimobjectIt cannot be used without specifying the position and size properties.
images.options.trim.xnumberThe X coordinate of the starting point of the crop.
images.options.trim.ynumberThe Y coordinate of the starting point of the crop.
images.options.trim.widthnumberThe width of the cropped image.
images.options.trim.heightnumberThe height of the cropped image.
images.options.sizeobjectIt cannot be used without specifying the position property.
images.options.size.widthnumberThe applied width of the image (you can stretch or compress the image).
images.options.size.heightnumberThe applied height of the image (you can stretch or compress the image).
initbooleantrueInitializes the library in full. Starts loading images and sets the canvas dimensions.
directionnumber1Frame change direction. 1 is forward, -1 is back.
loopbooleantrueLooping rendering.
fpsnumber60Number of frames per second. Range from 1 to 60.
startIndexnumber0The lower border of the render.
finishIndexnumberimages.length - 1The upper border of the render.
currentIndexnumber0Specifies which index in the image array is currently being rendered. During initialization, it will call the play method, not start if it differs from the startIndex property.
startImmediatelybooleanfalseStart immediately after loading the first image.
startAfterLoadedbooleantrueStarts rendering immediately after loading all images.
loggingbooleanfalseOutput to the console information about the rendering of a certain image.

Methods

MethodDescription
startStarts rendering from the image index specified in startIndex.
playPlays rendering.
pausePauses rendering.
stopStops rendering and draws images with index startIndex.
onAdds an event listener.
offRemoves an event listener.
setCurrentImageSets the value for the currentImage properties. Make sure that the value does not exceed the length of the array.
getCurrentImageGets the value of the currentImage property.
setSizesCanvasSets the dimensions for the canvas { width, height }.
getSizesCanvasGets canvas settings { width, height }.
setImageOptionsSets new settings for images { position?: { x, y }, trim?: { x, y, width, height }, size?: { width, height } }.
getImageOptionsGets image settings { position: { x, y }, trim: { x, y, width, height }, size: { width, height } }.
setFpsSets the value for the fps properties. Range from 1 to 60.
getFpsGets the value of the fps property.
setDirectionSets the value for the directions properties. The value can be 1 or -1.
getDirectionGets the value of the directions property.
setLoopSets the value for the loop properties, boolean value only.
getLoopGets the value of the directions property.
setStartIndexSets the value for the startIndex properties. Make sure that the value does not exceed the length of the array.
getStartIndexGets the value of the startIndex property.
setFinishIndexSets the value for the finishIndex properties. Make sure that the value does not exceed the length of the array.
getFinishIndexGets the value of the finishIndex property.
getLoggingGets the value of the logging property.
enableLoggingEnable logging.
disableLoggingDisable logging.

Usage of methods

const canvas = new SequenceCanvas({
  // ...
})
//...
canvas.pause();

Events

EventDescription
initInitialization.
loadImage loading has started.
loadedFinished loading the image.
renderThe picture is drawn on the canvas.
startThe start method is called, the rendering of images has started from the index specified in startIndex.
playThe play method is called, the rendering of images has started from the index specified in currentIndex.
pauseThe pause method is called, rendering is on pause.
stopThe stop method is called, currentIndex borrows a value from startIndex.

Usage of events

const canvas = new SequenceCanvas({
  // ...
  on: {
    'init': function(event) {
      console.log('init', event);
    }
  }
});
// ...
function pauseHandler() {
  console.log('pause');
}
canvas.on('pause', pauseHandler);
canvas.off('pause', pauseHandler);

Authors

License

MIT

1.0.0

5 months ago

0.2.4

6 months ago

0.2.3

6 months ago

0.2.2

6 months ago

0.2.1

6 months ago

0.2.0

6 months ago

0.1.2

6 months ago

0.1.1

6 months ago

0.1.0

6 months ago