| space, → | next slide |
| ← | previous slide |
| d | debug mode |
| ## <ret> | go to slide # |
| c | table of contents (vi) |
| f | toggle footer |
| r | reload slides |
| z | toggle help (this) |
//assign DOM element to var
var canvas = document.getElementById('canvasarea');
//if browser supports HTML5 assign context
if (canvas.getContext){
var ctx = canvas.getContext('2d');
}
<!--Mark-up for Canvas element-->
<canvas id="tutorial" width="150" height="150"></canvas>There are various types e.g.
If a browser supports canvas it minimally supports "2d" context
Context supplies you with canvas methods you can use
Image courtesy of diveintohtml5.org
Same coordinate system as flash
//Create instance of Loader Class
var my_loader:Loader = new Loader();
//New URLRequest class
//Run load method on loader for request
my_loader.load(new URLRequest("myImage.jpg"));
//Add loader to stage
addChild(my_loader);
//Create instance of Image Class
var image = new Image();
//Assign image source
image.src = "myImage.jpg";
//Bare bones minimum for drawing image
drawImage(image, x, y);
package
{
public class Main
{
//Assign class vars
var Blah;
public function Main()
{
//Do stuff...
}
}
}
//Assign public "global" vars
var Blah;
window.onload = function(){
//Do Stuff...
}
package
{
public class Main
{
//Assign class vars
var Blah;
public function Main()
{
init();
}
private function init(){
//Do stuff...
}
}
}
//Launch into publicly
//accesible Main function
var Blah;
var Main;
window.onload = init;
function init()
{
new Application();
}
function Application()
this.startApplication = function(){
/Do stuff...
}
}
function myClass(){
}
new myClass();
myClass = {
testFunction : function(){}
}
new myClass();
function myClass(){
this.myPublicVar = "meow";
var meowzers = "BOOM";
}
console.log(new myClass().myPublicVar); // Logs "meow"
console.log(new myClass().meowzers); // Logs "undefined"
function ExtendedFrom(){
this.testItem = "Hello";
}
function ExtendedTo(){
}
ExtendedTo.prototype = new ExtendedFrom();
console.log(new ExtendedTo().testItem); // Logs "Hello"
const FPS = 60;
var canvas = null;
var context2D = null;
window.onload = function() {
init();
}
function init()
{
canvas = document.getElementById('canvasarea');
if (canvas.getContext){
context2D = canvas.getContext('2d');
}
setInterval(draw, 1000 / FPS);
}
function draw()
{
}
const FPS = 60;
var x = 50;
var y = 0;
var xDirection = 1;
var yDirection = 1;
var canvas = null;
var context2D = null;
window.onload = function() {
init();
}
function init()
{
canvas = document.getElementById('canvasarea');
if (canvas.getContext){
context2D = canvas.getContext('2d');
}
setInterval(draw, 1000 / FPS);
}
function draw()
{
context2D.clearRect(0, 0, 300, 300);
context2D.beginPath();
context2D.arc(x,y,5,0,Math.PI*2, true);
context2D.fill();
x += 1 * xDirection;
y += 1 * yDirection;
if (x >= canvas.width)
{
x = canvas.width;
xDirection = -1;
}
else if (x = 0)
{
x = 0;
xDirection = 1;
}
if (y >= canvas.height)
{
y = canvas.height;
yDirection = -1;
}
else if (y = 0)
{
y = 0;
yDirection = 1;
}
}
beginPath() closePath() stroke() fill() moveTo(x, y) lineTo(x, y) arc(x, y, radius, startAngle, endAngle, anticlockwise) rect(x, y, width, height) quadraticCurveTo(cp1x, cp1y, x, y) bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y) fillRect(x,y,width,height) strokeRect(x,y,width,height) clearRect(x,y,width,height) //Even has access to the pixel array context.getImageData(left, top, width, height) //Similar to AS3s BitmapData(width, height, transparent, fillColor)
const FPS = 260;
var x = 50;
var y = 0;
var xDirection = 1;
var yDirection = 1;
var canvas = null;
var context2D = null;
var image = new Image();
image.src = "dino.png";
var degrees = 10;
window.onload = function() {
init();
}
function init()
{
canvas = document.getElementById('canvasarea');
if (canvas.getContext){
context2D = canvas.getContext('2d');
}
setInterval(draw, 1000 / FPS);
}
function draw()
{
context2D.clearRect(0, 0, canvas.width, canvas.height);
context2D.save();
var radians = degrees++ * Math.PI / 180;
context2D.rotate(radians);
context2D.drawImage(image, 0,y++, 148, 52, 0, 0, 148, 52);
context2D.restore();
}
function draw()
{
context2D.clearRect(0, 0, canvas.width, canvas.height);
var radians = degrees++ * Math.PI / 180;
context2D.rotate(radians);
context2D.drawImage(image, 0,0, 148, 142, 0, 0, 148, 142);
}
Crazy town ensues...
translate(x, y) rotate(angle) scale(x, y) transform(m11, m12, m21, m22, dx, dy) //Crazy transformation matrix
drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight)
Image courtesy of developer.mozilla.org
Perfect for showing just parts of an image
Normal Sprite
Canvas
const FPS = 12;
var x = 50;
var y = 0;
var xDirection = 1;
var yDirection = 1;
var canvas = null;
var context2D = null;
var image = new Image();
image.src = "build4.png";
var frames = 12;
var frameOffset;
var offSetArea = 0;
window.onload = function() {
init();
}
function init()
{
canvas = document.getElementById('canvasarea');
if (canvas.getContext){
context2D = canvas.getContext('2d');
}
frameOffset = Math.ceil(image.width/frames);
setInterval(draw, 1000 / FPS);
}
function draw()
{
if(offSetArea > image.width){
offSetArea = 0;
}
offSetArea += frameOffset;
context2D.clearRect(0, 0, canvas.width, canvas.height);
context2D.drawImage(image, offSetArea,0, image.height,
frameOffset, 0, 0, image.height, frameOffset);
}
Solution? Figure out where was clicked in the canvas and compare to area of desired interaction
const FPS = 12 ;
var x = 50;
var y = 0;
var xDirection = 1;
var yDirection = 1;
var canvas = null;
var context2D = null;
var image = new Image();
image.src = "build4.png";
var frames = 12;
var frameOffset;
var offSetArea = 0;
window.onload = function() {
init();
}
function init()
{
canvas = document.getElementById('canvasarea');
///****ADD LISTENER****///
canvas.addEventListener("click", canvasClick, false);
if (canvas.getContext){
context2D = canvas.getContext('2d');
}
frameOffset = Math.ceil(image.width/frames);
setInterval(draw, 1000 / FPS);
}
function draw()
{
if(offSetArea > image.width){
offSetArea = 0;
}
offSetArea += frameOffset;
context2D.clearRect(0, 0, canvas.width, canvas.height);
context2D.drawImage(image, offSetArea,0, image.height,
frameOffset, 0, 0, image.height, frameOffset);
}
function canvasClick(e) {
console.log(getCursorPosition(e).x); //Logs X position
console.log(getCursorPosition(e).y); //Logs Y position
}
function getCursorPosition(e) {
var xposition;
var yposition;
if (e.pageX != undefined e.pageY != undefined) {
xposition = e.pageX;
yposition = e.pageY;
}
else {
xposition = e.clientX + document.body.scrollLeft +
document.documentElement.scrollLeft;
yposition = e.clientY + document.body.scrollTop +
document.documentElement.scrollTop;
}
xposition -= canvas.offsetLeft;
yposition -= canvas.offsetTop;
function position(xpos, ypos)
{
this.x = xpos;
this.y = ypos;
}
var pos = new position(xposition,yposition);
return pos;
}
-webkit-transform:
-ms-transform:
-moz-transform:
-o-transform:
//Transform Functions
rotate(angle);
scale(sx[, sy]);
skew(ax[, ay]);
translate(tx[, ty]);
matrix(a, c, b, d, tx, ty); //Crazy transformation matrix
-moz-transition-property
-moz-transition-duration
-moz-transition-timing-function
-moz-transition-delay
//or altogether:
-webkit-transition: property duration timing_function delay [, ...];
-moz-transition: property duration timing_function delay [, ...];
-webkit-animation-name: [NAME];
-webkit-animation-iteration-count: [number, infinite];
-webkit-animation-timing-function: [linear, ease,ease-in, ease-out];
-webkit-animation-duration: [time];
-webkit-animation-delay: [time];
css-selector{
-webkit-animation: NAME-OF-ANIMATION duration delay iterations fillmode direction;
}
@-webkit-keyframes slideInRightToLeft {
from {
-webkit-transform: translateX(100%);
}
to {
-webkit-transform: translateX(0);
}
}
nodeType_to_target:target{
-webkit-animation: NAME-OF-ANIMATION duration delay iterations fillmode direction;
}
<a href="#id1">Mooooove</a>