290 likes | 513 Views
Neal Stublen nstublen@jccc.edu. HTML5 and CSS3. Chapter 11 Canvas, SVG, and Drag and Drop (JavaScript Ahead). Canvas. Why Use a Canvas?. Flexibility… Use of drawing primitives (lines, arcs, text) Direct access to element display pixels Use a <canvas> element in your HTML
E N D
Neal Stublen nstublen@jccc.edu HTML5 and CSS3
Why Use a Canvas? • Flexibility… • Use of drawing primitives (lines, arcs, text) • Direct access to element display pixels • Use a <canvas> element in your HTML • Supply an “id” attribute to access the element from JavaScript
Canvas Coordinate System • Upper-left corner at (0, 0) • Lower right at (width, height)
Filling a Rectangle $('document').ready(function() { var canvas = document.getElementById('mycanvas'); varcontext = canvas.getContext('2d'); context.fillStyle= 'rgba(0, 0, 255, 0.5)'; context.fillRect(10, 10, 100, 100); context.strokeStyle = 'red'; context.strokeRect(10, 10, 100, 100); });
Filling with an Image $('document').ready(function() { varimg = new Image(); img.src = ‘./images/bg-bike.png’; img.onload = function() { pattern = context.createPattern(img, 'repeat'); context.fillStyle = pattern; context.fillRect(10, 10, 100, 100); context.strokeRect(10, 10, 100, 100); }; });
Filling with a Gradient $('document').ready(function() { vargradient = context.createLinearGradient(0, 0, 0, 200); gradient.addColorStop(0, 'blue'); gradient.addColorStop(1, 'white'); context.fillStyle= gradient; context.fillRect(10, 10, 100, 100); context.strokeRect(10, 10, 100, 100) });
Drawing with Paths • We’ve seen drawing rectangles • We can draw other shapes, but we need to define a “path” for the shape
Drawing with Paths $('document').ready(function() { context.beginPath(); context.arc(50, 50, 30, 0, Math.PI * 2, true); context.closePath(); context.strokeStyle= 'red'; context.fillStyle = 'blue'; context.lineWidth = 3; context.fill(); context.stroke(); };
Experiment • Experiment inside the browser • http://www.html5canvastutorials.com/
Manipulating Pixels varimage = document.getElementById('myimage'); context.drawImage(image, (canvas.width - image.width) / 2, (canvas.height - image.height) / 2);
Manipulating Pixels varimageData= context.getImageData(0, 0, canvas.width, canvas.height); varpixelData = imageData.data; for (vari = 0; i < pixelData.length; i += 4) { varr = pixelData[i]; varg = pixelData[i+1]; varb = pixelData[i+2]; vargray = r * 0.3 + g * 0.59 + b * 0.11; pixelData[i] = gray; pixelData[i+1] = gray; pixelData[i+2] = gray; } context.putImageData(imageData, 0, 0);
Displaying Text context.textAlign = 'left'; context.font = '18px LeagueGothic'; context.fillText('Hello!');
Scalable Vector Graphics • Describe vector graphics using XML • Preserves shape when size changes • Performs many tasks similar to canvas • Gradients, paths, shapes, text • XML descriptions instead of JavaScript
An SVG Circle <svgxmlns="http://www.w3.org/2000/svg" viewbox="0 0 400 400”> <circle cx="50" cy="50" r="25" fill="red“ /> </svg>
An SVG Rectangle <svgxmlns="http://www.w3.org/2000/svg" viewbox="0 0 400 400"> <rect x="10" y="10" width="100" height="100" fill="blue" stroke="red" stroke-width="3" /> </svg>
SVG Complexities • SVG drawings can become very complex • Inkscape – Free vector graphics editor • http://www.inkscape.org • Raphael – JavaScript library for working with SVG • http://raphaeljs.com
Why SVG? • SVG can provide a much smaller description of an image (smaller files) • Rather than describing the RGBA values of each pixel, it can describe lines and shapes regardless of size • Scaling doesn’t produce artifacts • No jagged edges
Canvas or SVG? • Canvas provides pixel manipulation • Canvas provides saving images to PNG and JPEG files • SVG provides DOM access to drawing elements • SVG has more tools
Steps to Drag and Drop • Set the draggable attribute on any elements you want to be draggable • Set an event listener for the dragstart event on any draggable elements • Set an event listener for the dragover and drop events on any elements that can accept draggable elements
Set Draggable • Set the draggable attribute on an element <div draggable="true"></div> • This is not a boolean attribute
Watch for dragstart $('.src').bind('dragstart', function(event) { event.originalEvent .dataTransfer .setData('text/plain', event.target.getAttribute('id')); });
Watch for dragover $(‘.tgt').bind('dragover', function(event) { event.preventDefault(); });
Watch for drop $(‘.tgt').bind('drop', function(event) { varsrc = event.originalEvent .dataTransfer.getData("text/plain"); });
Cat and Mouse • HTML Herald supports drag and drop at the cat article • Drag the mice onto the cat
DraggableDivs • Create a series of colored div elements that can be dragged onto other divs to change their color • Swap position of colored divs by dragging them onto one another