Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Canvas Element

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

Canvas Element

Avatar for Davidson Fellipe

Davidson Fellipe

April 24, 2012
Tweet

More Decks by Davidson Fellipe

Other Decks in Programming

Transcript

  1. o que é o elemento canvas? criado pela apple para

    os widgets do Dashboard OS X desenhos bitmap 2D, 3D (futuro) html+javascript especificação w3c implementado para o Safari e Chrome. browsers baseados no Gecko 1.8 também suportam.
  2. canvas vs. svg performance desenhos 2D salvar resultado em png

    adequado para gráficos, imagens e manipulaçoes de pixels desempenho constante independente de resolução controle dos elementos via DOM baseado em XML processamento lento a medida que aumenta a complexidade da DOM http://borismus.com/canvas-vs-svg-performance/
  3. rotacionando um imagem <!DOCTYPE HTML> <html> ! <head> ! </head>

    ! <body> ! ! <img src="teste.jpg" id="image" style="display:none;width:500px;height:375;" /> ! ! <canvas id="simpleCanvas" width="500" height="375"> ! Seja mais feliz, não use IE! ! ! </canvas> ! ! <script type="text/javascript"> //proximo slide ! ! </script> ! </body> </html>
  4. rotacionando um imagem window.onload = function(){ ! var canvas =

    document.getElementById('simpleCanvas'); ! if (canvas && canvas.getContext) { ! ! var image = document.getElementById('image'); ! ! canvas.height = image.width; ! ! canvas.width = image.height; ! ! var context = canvas.getContext('2d'); ! ! //mover de um ponto a outro do grid ! ! context.translate(image.height, 0); ! ! context.rotate(Math.PI / 2); ! ! context.drawImage(image, 0, 0); ! ! context.restore(); ! } };
  5. imprimindo pontos <!DOCTYPE HTML> <html> ! <head> ! ! <style

    type="text/css" media="screen"> ! ! ! #simpleCanvas{height:520px;width:520px;} ! ! </style> ! </head> ! <body> ! ! <canvas id="simpleCanvas"> ! ! ! Seja mais feliz, não use IE! ! ! </canvas> ! ! <script type="text/javascript"> window.onload = function(){ //proximo slide }; ! ! </script> ! </body> </html>
  6. ! ! var data = [{x: 0,y:0}, ! ! !

    ! ! ! ! {x: 50,y:50}, ! ! ! ! ! ! ! {x: 100,y:120}, ! ! ! ! ! ! ! {x: 100,y:200}, ! ! ! ! ! ! ! {x: 300,y:200}, ! ! ! ! ! ! ! {x: 400,y:100}]; ! ! ! ! ! ! var canvas = document.getElementById('simpleCanvas'); imprimindo pontos
  7. //configurar largura do canvas canvas.height = canvas.clientHeight; canvas.width = canvas.clientWidth;

    ! ! ! ! ! ! ! //caracteristicas do chart var padding = 10; var widthChart = canvas.width - (2*padding); var heightChart = canvas.width - (2*padding); imprimindo pontos //tamanho marcadores var size_x = 10; var size_y = 10; var axis_x = 0; var axis_y = 0;
  8. context.fillStyle = "#fff"; context.strokeStyle = "#CCCCCC"; context.lineWidth = 20; context.strokeRect(0,

    0, canvas.width, canvas.height); context.fillRect(10, 10, widthChart, heightChart); context.fill(); var marginLine = 0; context.lineWidth = 1; imprimindo pontos
  9. //descrever linhas ! ! for(var numberLine = 0; numberLine <

    10; numberLine ++) { ! ! ! ! ! ! marginLine = numberLine * (widthChart/10) + padding; context.moveTo(marginLine, padding); context.lineTo(marginLine, canvas.height - padding); context.moveTo(padding, marginLine); context.lineTo(canvas.width - padding, marginLine); } ! ! ! ! ! //imprimir linhas context.strokeStyle = "#eee"; context.stroke(); imprimindo pontos
  10. //imprimindo os pontos for(a in data){ axis_x = data[a].x +

    padding - (size_x/2); axis_y = canvas.height - (data[a].y - (size_y/2)) - 2*padding; context.fillStyle = '#cc0000'; context.fillRect(axis_x, axis_y, size_x, size_y); } imprimindo pontos
  11. //imprimindo o texto context.font = 'italic 400 20px Georgia, serif';

    context.fillStyle = "#666"; ! ! context.fillText("BrazilJS - Fortaleza 2011", 30, 40, 140); ! ! context.textAlign = "center"; imprimindo pontos