[](https://www.npmjs.com/package/@ff0000-ad-tech%2Fad-canvas) [](https://www.npmjs.com/package/@ff0000-ad-tech%2Fad-canvas)






*
This lightweight package is a simple-to-use tool that makes it easy to create animation, masking, layering, blend modes,
and drawing all sorts of graphical content, such as:
- Vector paths
- Shapes
- Bitmaps
- Videos
- Text
Read more about some of the ways this framework compares to vanilla javascript in the Why
CanvasDrawer? section.
It may be beneficial to familiarize yourself with the syntax of
ad-ui, as there are many similarities. Or, this can be
your stepping stone to ad-ui!
Make a new CanvasDrawer instance - this instance will house whatever elements you create
``javascript`
let myCanvasDrawer = new CanvasDrawer({
target: myDomElement,
css: {
width: 300,
height: 250
},
debug: true,
retina: true
})
In this example, we create a new UICanvas DOM element (which can be referenced as myCanvasDrawer.canvas).retina: true
Setting means the canvas will render at double-density. Setting debug: true adds a backgroundcanvas
color so that we can see the element, which is transparent otherwise.
CanvasDrawer has an update() method which must be called any time you make a change and want to see it.
A CanvasElement is the broad term for anything that is drawn within the CanvasDrawer framework. It refers to:
- CanvasImage - use a bitmap, canvas, CanvasDrawer, or video source to make image data
- CanvasRect - create a rectangle
- CanvasCircle - create an arc, semi circle, or circle
- CanvasPath - use points and curves to create a pathed shape
- CanvasText - write text into a canvas
Draw a bitmap data into a CanvasDrawer. In this example, the source is referenced by the name of the bitmap, as for instance.
ID'd by ImageManager - this might be "myBitmap.png"
``javascript
let myImage = new CanvasImage({
target: myCanvasDrawer,
id: 'myCanvasImage'
source: "myBitmap",
params: {
width: 300,
height: 250
}
})
// setting the alpha by ID
myCanvasDrawer.elements.myCanvasImage.alpha = 0.5
// setting the alpha by variable
myImage.alpha = 0.5
// re-render the CanvasDrawer to ensure update is seen
myCanvasDrawer.update()
`
These CanvasElements can have features such as:
- drop shadows
- rotation, scale, alpha
- strokes
There are also additional classes that, if imported, will open up other features to you, such as:
- blur
- masking
- tweening
- patterns
- gradients
- blend modes
- color transformation effects
Tweening a CanvasElement is as easy as initializing it through CanvasTweener - it's an additional load that you
might not want, so we give you the option.
`javascript
// injects tweening capability into a CanvasDrawer instance
CanvasTweener.init(myCanvasDrawer)
// call the CanvasElement to tween by a variable definition
myCanvasDrawer.tween.to(myImage, 2, { rotation: 90, alpha: 0 })
// call the CanvasElement to tween by an ID
myCanvasDrawer.tween.to("myCanvasImage", 2, { rotation: 90, alpha: 0 })
myCanvasDrawer.tween.fromTo(myShape, 2, { scale: 0 }, { scale: 1, ease: Back.easeOut })
myCanvasDrawer.tween.start()
`
This tween object that is part of your CanvasDrawer instance uses the same format as GreenSock's TweenCanvasTweener
Package. However, since HTML5 canvas requires refreshing in order to render changes, takes care of that for us without having to write update() calls elsewhere. ThemyCanvasDrawer.tween.start() method ensures these tweens don't begin until we're ready to call all of them.
- Using every nook and cranny the CanvasDrawer suite has to offer only adds an additional _40k_ to your file
size.
- Compare that to other packages which are _150k or higher_, and you have a fast-loading platform with powerful
features giving you the best possible tools for quick development and stunning animations.
- The classes that allow for blurring, color transformations, etc are there to save you, the user, the effort of
researching how to build and keep track of these effects.
- With vanilla Canvas2D, animation becomes complicated. There are no native object declarations or stored values:
you must work that logic out yourself, and constantly call verbose code in order to render graphics.
Compare some vanilla canvas drawing to how it might look using CanvasDrawer:
`javascript
// any time you want to draw the image at a different X, Y, width, or height,
// you need to clear your canvas ...
ctx.clearRect(0, 0, 100, 200)
// and if you want to scale, rotate, or change the alpha, that's another kind of logic
// you must develop code for and
manage yourself
ctx.save()
ctx.globalAlpha = 0.5
ctx.rotate(Math.PI)
ctx.restore()
// finally, make a verbose call to the context to draw all images you may have created
ctx.drawImage(img1, 10, 10, 100, 200)
ctx.drawImage(img2, 0, 0, 50, 50)
ctx.drawImage(img3, 100, 30, 60, 60)
// and how do you animate this? How do you get your image to move its x-coordinate using TweenLite?
TweenLite.to(???, 1, {x: 200, onUpdate: function(){
ctx.clearRect(...)
ctx.drawImage(...)
}})
// sounds exhausting, and you're going erase every other image you added unless you put in
// additional conditions to re-draw them.
`
CanvasDrawer makes the simple act or redrawing and moving elements easy, saving you file size and processing by
packaging the creation and storage of a canvas' drawable data in objects easy to manipulate:
`javascript
myCanvasImage1.x = 10
myCanvasImage2.rotation = 20
myCanvasImage3.alpha = 0.5
myCanvasDrawer.tween.to(myCanvasImage1, 1, { x: 200 })
myCanvasDrawer.tween.start()
// easy peasy, lemon squeezy
``
*
stringstringstringstringstringstringstringstringstringstringstringstringstringstringstringstringstringstringstringstringstringstringstringstringstringstringnumbernumbernumbernumbernumbernumbernumbernumbernumbernumberbooleanstringstringstringnumberstringstringstringnumbernumbernumberstringnumbernumbernumberstringnumbernumbernumbernumberCanvasDrawer \| UICanvas \| canvasCanvasDrawer \| UICanvas \| canvasCanvasDrawer \| UICanvas \| canvasnumbernumbernumbernumberCanvasDisplaceChannel \| numberCanvasDisplaceChannel \| numberUICanvasbooleannumberobjectnumbernumbernumbernumbernumbernumbernumbernumbernumbernumbernumbernumbernumbernumbernumbernumbernumbernumberstringstringnumbernumbernumberstringstringstringstringstringstringnumbernumbernumbernumbernumbernumbernumbernumberbooleannumberstringstringstringnumberstringstringstringnumbernumbernumberstringnumbernumbernumberstringnumbernumbernumbernumbernumbernumbernumbernumbernumbernumbernumbernumbernumbernumbernumbernumberstringstringstringnumberstringstringstringnumbernumbernumberstringnumbernumbernumberstringnumbernumbernumbernumbernumbernumbernumbernumbernumbernumbernumberbooleanstringstringnumbernumbernumberstringstringstringnumberstringstringnumbernumbernumberstringnumbernumbernumberstringnumbernumberstringstringstringstringobject
*