KenBurnsBase
December 15, 2016 ยท View on GitHub
KenBurns base class. 2 methods must be implemented: draw() and getViewport().
Properties
clampedboolean guarantees the rendering never goes out of bound. (default:true).backgroundRGBA background color as an Array of 4 numbers (in 0..1 range). (default:[0,0,0,0], transparent)
animate
Start a KenBurns animation on source in a loop for the next duration milliseconds from fromCrop to toCrop with given easing function.
sourceMUST be ready (e.g. image loaded) before callinganimate. Also, the animation is not interruptable, For better controls, please useanimateStep().
Parameters
sourceSource , the source to animate (the type depends on implementation, refer to implementation 'supported source')fromCrop(CropFunc | Bound) , an array of[ x, y, width, height ]which is the starting bound absolute coordinate. OR a(viewportSize,imageSize)=>Boundwhich returns this array (the viewportSize and imageSize parameters have width and height fields).KenBurns.crop()provide such a function.toCrop(CropFunc | Bound) , same asstartCropbut for the ending bound coordinate.durationnumber , the duration in millisecond of the animation.easingEasingFunc? , The easing function to use for the animation. We recommend the use of Bezier-Easing.
Returns Promise<Source> a Promise that is resolved when the animation ends.
animateStep
Perform a single frame of a Kenburns animation on source from fromCrop to toCrop with a progress interpolation value.
animateStepprovides a better rendering control thananimatebut also is low level. It's up to you to handle the animation loop.
Parameters
sourceSource (same as inanimate)fromCrop(CropFunc | Bound) (same as inanimate)toCrop(CropFunc | Bound) (same as inanimate)progressnumber the interpolation value from 0 to 1.
Returns void
draw
(abstract method) Draw the source cropped to a given rectangle bound.
Type: function (source: Source, rect: Bound): void
getViewport
(abstract method) Get and object with width and height of the container.
Type: function (): WidthHeight
getSourceSize
(abstract method) Get source size.
Type: function (source: Source): WidthHeight
KenBurnsDOM
Extends KenBurnsBase
DOM Kenburns effect.
Supported source: any HTML Element or an Image.
NB: the container & source DOM element style are getting mutated by KenBurnsDOM. Make sure you don't alter them while performing the effect.
Examples
import KenBurnsDOM from "kenburns/lib/DOM";
var div = document.createElement("div");
div.style.width = "400px";
div.style.height = "400px";
const kenBurnsDOM = new KenBurnsDOM(div);
// kenBurnsDOM.animate(...).then(...);
// kenBurnsDOM.animateStep(...);
constructor
Create a DOM KenBurns with a container element (block element recommended).
Parameters
containerHTMLElement
KenBurnsWebGL
Extends KenBurnsBase
WebGL KenBurns implementation.
supported source: a gl-texture2d instance. (or an object with a bind() function and shape array)
Examples
import KenBurnsWebGL from "kenburns/lib/WebGL";
var canvasWebGL = document.createElement("canvas");
canvasWebGL.style.width = "400px";
canvasWebGL.style.height = "400px";
canvasWebGL.width = 800;
canvasWebGL.height = 800;
const gl = canvasWebGL.getContext("webgl");
const kenBurnsWebGL = new KenBurnsWebGL(gl);
// import createTexture from "gl-texture2d";
// const texture = createTexture(gl, ...);
// kenBurnsWebGL.animate(texture, ...).then(...);
// kenBurnsWebGL.animateStep(texture, ...);
constructor
Create a KenBurnsWebGL with a WebGL Context.
Parameters
dispose
clean up object created by the implementation. This allows to potentially continue using the WebGL Canvas for other things.
KenBurnsCanvas2D
Extends KenBurnsBase
Canvas2D KenBurns implementation.
supported source: Image | Video | Canvas2D
Examples
import KenBurnsCanvas2D from "kenburns/lib/Canvas2D";
var canvas2d = document.createElement("canvas");
canvas2d.style.width = "400px";
canvas2d.style.height = "400px";
canvas2d.width = 800;
canvas2d.height = 800;
const ctx = canvas2d.getContext("2d");
const kenBurnsCanvas2d = new KenBurnsCanvas2D(ctx);
// kenBurnsCanvas2d.animate(...).then(...);
// kenBurnsCanvas2d.animateStep(...);
constructor
Create a Canvas2D KenBurns with a Canvas 2D Context.
Parameters
crop
import crop from"react-crop";
To ease the process of providing fromCrop and endCrop, crop is a helper that that computes the absolute croping rectangle for a given center and zoom.
Parameters
zoomRatio: The first parameter is the zoom ratio. (this zoom ratio must be in]0.0, 1.0]range) For instance, 1.0 display the full image size, 0.01 display a very tiny part of the image.center: The second parameter is the center of the effect (the [w,h] components are in percentage from 0.0 to 1.0). This parameter is optional, if not provided, the center is used.
Examples
var from = crop(1.0, [0.5, 0.5]);
var to = crop(0.2, [0.8, 0.5]);
kenBurns.animate(img, from, to, 4000);
// Checkout also this special value: `crop.largest`
Bound
An array of [x, y, width, height]
Type: [number, number, number, number]
RGBA
A background color as an array of 0..1 numbers.
Type: [number, number, number, number]
WidthHeight
An object with a width and height property.
Type: {width: number, height: number}
Properties
CropFunc
A function that returns a bound crop for a given viewport and source size.
Type: function (viewport: WidthHeight, sourceSize: WidthHeight): Bound
EasingFunc
An easing function.