16
loading...
This website collects cookies to deliver better user experience
logo_circle.animate();
logo_circle.reset();
<div id="stage"></div>
<button id="restart">Restart</button>
set the dimensions and border of the stage container
give the stage container position:relative so that our canvas layers will be stacked inside it
style the body to center our elements
allow canvas layers to stack on top of each other by giving them position:absolute
let utils = {};
let utils = {};
(function(){
})();
let utils = {};
(function(pen){
})(utils);
utils.setStage('stage');
utils.setBackgroundColor('lightpink');
(function(pen) {
let stage = null;
})(utils);
(function(pen) {
let stage = null;
pen.setStage = (stageId) => {
stage = document.getElementById(stageId);
};
})(utils);
(function(pen) {
let stage = null;
pen.setStage = (stageId) => {
stage = document.getElementById(stageId);
};
pen.setBackgroundColor = function(color) {
if (stage) {
stage.style.backgroundColor = color;
}
}
})(utils);
creating an array for tracking animation frame requests
creating a new canvas element for each graphic, setting its properties, and appending it to the stage
obtaining our context — essentially a bundle of drawing methods for the new canvas
adding canvas and context as properties to our graphic object
adding all properties from the passed-in object
setting a new cur property for storing the state of the graphic
pen.graphic = function(o={}) {
};
pen.graphic = function(o={}) {
this.reqIds = [];
}
pen.graphic = function(o={}) {
this.reqIds = [];
this.canvas = document.createElement('canvas');
}
this.canvas.width = stage.clientWidth;
this.canvas.height = stage.clientHeight;
this.canvas.setAttribute('role','img');
if (o.label) {
this.canvas.setAttribute('aria-label', o.label);
}
if (o.zIndex) {
this.canvas.style.zIndex = o.zIndex;
}
stage.appendChild(this.canvas);
this.ctx = this.canvas.getContext('2d');
for (key of Object.keys(o)) {
his[key] = o[key];
}
this.cur = {...this.start};
four render methods that can draw out the shape, text, or image based on its object’s properties
updateProps, which updates the properties of the object before the next animation frame is drawn
checkCanvasAlpha, which updates the globalAlpha property of the object’s canvas rendering context if its alpha value is changing
animate, which changes how the object is represented on its canvas —and then calls itself if another frame is needed
cancelFrames, which cancels any remaining frame requests if our animation has been restarted
reset, which returns the object and its canvas to their original states
pen.graphic.prototype.methodname = function() {
};
create a new Image() object,
set its src attribute,
listen for an image onload event,
and call therender() function when the image loads.
pen.graphic.prototype.updateProps = function() {
for (prop in this.speed) {
}
}
let diff = Math.abs(this.cur[prop] - this.stop[prop]);
if (diff <= Math.abs(this.speed[prop])) {
this.cur[prop] = this.stop[prop];
}
else {
this.cur[prop] += this.speed[prop]
}
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
if (this.started) {
this.updateProps();
this.checkCanvasAlpha();
}
let func = this.type + 'Render';
this[func]();
if (!this.started && this.delay) {
await sleep(this.delay);
}
this.started = true;
reset the started flag to false.
return all cur attributes to their starting values
sync the globalAlpha of the rendering context with the cur.alpha value, if it exists
call the cancelFrames method we just wrote.
pen.graphic = function(o = {}) {
// hidden code
for (key of Object.keys(o)) {
this[key] = o[key];
}
this.reset();
};
a circle that descends from above
a rectangle that rises from below
text saying “Nev’s” that slides in diagonally
text saying “Ice Cream” that fades in
a small ice cream cone image that fades in
a large three-scope cone that slides in from the right
text saying “42 Flavors!” that slides in from the left
let demo = {};
(function(pen) {
})(demo);
let demo = {};
(function(pen) {
utils.setStage('stage');
utils.setBackgroundColor('lightpink');
})(demo);
let demo = {};
(function(pen) {
utils.setStage('stage');
utils.setBackgroundColor('lightpink');
const colors = {
orange: '#ffc44e',
green: '#4795a7',
darkgreen: '#356f7d',
};
})(demo);
const small_cone_src = 'https://raw.githubusercontent.com/nevkatz/canvas-demos/main/demo1/images/cone-small-vert.png';
const large_cone_src = 'https://raw.githubusercontent.com/nevkatz/canvas-demos/main/demo1/images/ice-cream.png';
a descriptive label so screen readers pick it up.
aspeed property with x since it slides in horizontally from the right.
a scale property of 0.75 to make the image width and height 75% of the image’s natural dimensions.
a type property that determines the rendering method
a label property that sets the aria-label attribute of its element.
globalAlpha
property of the <canvas> element.pen.init = function() {
let arr = [logo_rect,
logo_circle,
flavors,
large_cone,
nevs,
ic_text,
small_cone];
};
for (o of arr) {
o.animate();
}
Grab references to the restart button and stage with querySelectorAll
Loop through both elements with for...of
For each element, listen for a click event.
Handle a click by calling animate and then reset on each JavaScript object.
let els = document.querySelectorAll('#restart,#stage');
for (el of els) {
el.addEventListener('click', function() {
for (o of arr) {
o.reset();
o.animate();
}
});
}