<head>
<title>Con canvas y a lo loco</title>
<script src="ejemplo.js"></script>
</head>
<body>
<canvas id="canvasBilbo" width="500" height="300">
<!-- medidas por defecto:
width = 300px
height = 150px -->
</canvas>
</body>
var canvas = document.getElementById("canvasBilbo");
var ctx = canvas.getContext("2d");
<canvas id="canvasBilbo" width="500" height="300"></canvas>
HTML
JavaScript
var canvas = document.getElementById("canvasBilbo");
var ctx = canvas.getContext("2d");
ctx.fillRect(25,25,100,100);
ctx.clearRect(45,45,60,60);
ctx.strokeRect(50,50,50,50);
<canvas id="canvasBilbo" width="500" height="300"></canvas>
HTML
JavaScript
var canvas = document.getElementById("canvasBilbo");
var ctx = canvas.getContext("2d");
ctx.moveTo(0,0); //configurar punto de salida
ctx.lineTo(200,100); //configurar path al punto final
ctx.stroke(); //dibujar una linea de 1px width
<canvas id="canvasBilbo" width="500" height="300"></canvas>
HTML
JavaScript
var canvas = document.getElementById("canvasBilbo");
var ctx = canvas.getContext("2d");
ctx.beginPath();
// X pos, Y pos, Radius, start point, end point
ctx.arc(95, 50, 40, 0, 2 * Math.PI);
ctx.stroke();
JavaScript
<canvas id="canvasBilbo" width="500" height="300"></canvas>
HTML
var canvas = document.getElementById("canvasBilbo");
var ctx = canvas.getContext("2d");
ctx.font = "30px Arial";
ctx.fillStyle = 'orange';
ctx.fillText("Hola Bilbao!",10,50);
<canvas id="canvasBilbo" width="500" height="300"></canvas>
HTML
JavaScript
var canvas = document.getElementById("canvasBilbo");
var ctx = canvas.getContext("2d");
// Creando degradado
var degra = ctx.createLinearGradient(0,0,200,0);
degra.addColorStop(0,"orange");
degra.addColorStop(1,"white");
// Relleno
ctx.fillStyle = degra;
ctx.fillRect(10,10,150,80);
JavaScript
<canvas id="canvasBilbo" width="500" height="300"></canvas>
HTML
var canvas = document.getElementById("canvasBilbo");
var ctx = canvas.getContext("2d");
var x = 0;
setInterval(function() {
ctx.fillStyle= "orange";
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillRect(x, 50, 50, 50);
x += 1;
}, 30);
JavaScript
<canvas id="canvasBilbo" width="500" height="300"></canvas>
HTML
var Class = function(){return function(){this.initialize.apply(this,arguments)}}
var Stage = Class();
Stage.prototype = {
initialize: function(canvasId, bgColor, rate) {
this.rate = Math.round( 1000 / rate );
this.canvas = document.getElementById("canvasBilbo");
this.context = this.canvas.getContext("2d");
this.bgColor = bgColor;
this.rect = this.canvas.getBoundingClientRect();
this.mouseX = 0;
this.mouseY = 0;
this.children = [];
var self = this;
this.canvas.onmousemove = function(e) {
self.mouseX = e.clientX - self.rect.left;
self.mouseY = e.clientY - self.rect.top;
};
},
//...
refresh:function(){
this.context.fillStyle = this.bgColor;
this.context.fillRect(0, 0, this.canvas.width , this.canvas.height );
},
addChild:function(obj){
this.refresh();
this.children.push( obj );
obj.stage = this;
obj.draw();
},
update:function(){
this.refresh();
for( var i = 0; i < this.children.length; i++ ) {
this.children[i].draw();
}
},
rendering:function(func){
var self = this;
setInterval( function(){
func();
self.update();
}, this.rate );
}
};
var Circle = Class();
Circle.prototype = {
initialize: function(radius, color) {
this.radius = radius;
this.color = color;
this.stage = null;
this.x = 0;
this.y = 0;
},
draw: function(){
if( this.stage == null ) return;
var ctx = this.stage.context;
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI*2, true);
ctx.closePath();
ctx.fill();
}
};
window.addEventListener("load", function(){
var stage = new Stage("canvas", "black", 50);
var circle = new Circle(10 , "orange");
stage.addChild(circle);
stage.rendering(function(){
circle.x += (stage.mouseX - circle.x) * 0.1;
circle.y += (stage.mouseY - circle.y) * 0.1;
});
});
JavaScript
Pintar con matemáticas
<head>
<script src="p5.min.js"></script>
<script src="sketch.js"></script>
</head>
HTML
function setup() {
createCanvas(400, 400);
}
function draw() {
background('orange');
}
JavaScript