The canvas is created with the canvas tag, but when they talk about it, they refer to the API, which includes a set of functions for drawing, lines, rectangles, circles, arcs, scaling, and transforming graphic elements.
In the case of a canvas on a web page, this is the rectangular area for the bitmap in which a color is assigned to each coordinate. Canvas JavaScript does not create vector objects in the style of other environments, such as SVG or Flash, but only raster as a picture.
In fact, you can upload an image to Canvas and manipulate its pixels one at a time. The service was recently included in HTML and is not supported by older browsers. It’s convenient for them to provide alternative content in the form of a fixed drawing or explanatory text.
Canvas vs. Flash Technology
Canvas JavaScript (canvas) is a standard HTML5 element handled by modern browsers. Flash is a company technology that requires the installation of a plugin. Most browsers have the Flash plugin installed today, so those that don’t support Canvas still function. However, the development dynamics indicate that the situation will change very soon, since the new platforms no longer work with Flash.
However, most graphics experts believe that working with Flash is easier and faster than working with canvas, because it has an advanced graphic design environment (Adobe Flash CS6), and the process of creating Canvas JavaScript graphics is somewhat more difficult. The situation should change with the advent of paper.js style frameworks. However, it should be noted that the new service is free and open, so you do not need to spend money on a license. Flash is owned by Adobe. This explains the fact that today complex games are still being created with it.
If you need to use graphics in several resolutions, the vector technology "Flash" is more effective than a raster Canvas. But for most web graphics, the canvas loads faster. Flash requires more resources to run, so Canvas JavaScript has a significant advantage on mobile devices. The canvas allows you to create graphics of all kinds, from a straight line to interactive games, and also allows you to edit images, change the tone and borders.
Attribute id
Canvas is an HTML element that allows you to dynamically create graphics and script animation. Its applications have a wide range of applications: games, interfaces, graphic editors, dynamic effects and 3D applications.
The main advantage of the service is that it does not require any additional plug-in, only one browser that supports JavaScript html5 Canvas: Safari, Chrome, Firefox, Opera and Internet Explorer.
For example, to draw two rectangles of different colors, use several API functions with JavaScript. First reserve the canvas space on the page using the HTML label, and then draw the shapes. The placement of the HTML html5 Canvas element is shown in the photo.
If desired, place others, such as a style, to indicate the attributes of the tables and determine their appearance.
Next, check the browser for compatibility with Canvas, prescribe the code.
Using getElementById (), we get the page element that is passed as the Canvas parameter. They then access the 2D canvas context and execute as many methods as the context needs to draw the Canvas JavaScript animation elements.
Next, write the full execution code.
Rectangle image
To draw a simple gray rectangle of 450 x 350 pixels, occupying the entire canvas, prescribe the code.
The width and height data determine the size of the surface. The position that this canvas occupies within the web will be determined by the place where the HTML element is written. Examples of JavaScript Canvas can be placed inside a table or inside a floating container.
If the platform does not support the style, the text “Your browser does not support Canvas” appears. Then place the tag with the capture of the drawing, which creates the canvas. To be able to refer to a specific element in Javascript code, it is convenient to give it an identifier, for example, id = miCanvas.
When an element is created, the JS code that makes the drawings is executed. This should be done after creating the canvas. One way to do this is to use the onLoad property. Once the page is loaded, the drawCanvas () function is executed.
The first thing to do is get the DOM node using the instructions: var canvas = document.getElementById ('miCanvas'), and pass in the identifier of the canvas element as a parameter. Next, get the JavaScript context object Canvas text: var context = canvas.getContext ('2d').
There are two possible contexts: 2d for drawing in two dimensions and webgl for an image in three dimensions. Having a context object, you can apply all the functions and properties associated with it.
Coordinate system
The origin is the point (0,0), the X coordinate values go to the right, and the Y coordinate values go down, in contrast to traditional coordinate systems. The placement of the object is performed relative to the origin, for example, the rectangle is in position (90.70). The Canvas canvas is 450 wide and 350 high for the line: context.fillRect (0, 0, 450, 350). A filled shape will be drawn with the upper right corner at the point (0,0), width 450 and height 350, that is, it will occupy the entire canvas of the example. To draw a rectangle without a fill, apply: strokeRect (x, y, width, height).
Canvas does not have specific functions for drawing polygons, including triangles and pentagons (except for a rectangle). The way to work with the canvas is to draw forms or paths. To build a form, first beginPath () is initialized, then the cursor is placed at the starting point using moveTo (x, y). There will be created a form with various features for the image.
Lines, arcs, and curves can be done using: stroke (), which draws an open or closed shape without a fill. If you compare the last point with the first, the form will be closed. Fill () draws a closed shape with a fill. If it was open, the function creates a line from the last to the starting point to complete it.
ClosePath Straight Line Tool
Use closePath () to create a line from the last to the starting point.
You can execute the previous code by copying and pasting it into the field, and then click the "Show written code" button.
There are three main methods for plotting curves:
- Arcos, arcs.
- Curvas de Bezier, Bezier curves.
- Curvas cuadráticas, quadratic curves.
The amplitude of the arc will go from the initial angle to the final. They are measured in radians. If the rotation direction parameter has a true logical value, the arc will go counterclockwise.
Bezier curves are a type of lines that are very often used in design and require a start, end, as well as some control points. The best way to get to know Bezier curves is to draw them using software such as Illustrator or Inkscape (free). You can also use the bezierCurveTo simulator (cp1x, cp1y, cp2x, cp2y, x, y), where cp1x, cp1y are the coordinates of the first control point, cp2x, cp2y are the second, and x and y coordinates of the end point of the curve.
Quadratic curves are similar to the previous ones, but have only one control point - quadraticCurveTo (cp1x, cp1y, x, y), where cp1x, cp1y are the primary coordinates, and x and y are the final coordinates.
The combination of forms and styles
To draw shapes, initialize the beginPath () function, place the cursor (moveTo (x, y)) to create shapes. To optionally close it, use closePath (). Drawing with fill () is done without fill and stroke. This process can be repeated as many times as necessary. The process is similar to drawing in pencil on a piece of paper. Each time the stroke is finished and the pencil is torn off the paper to place it in a new position, moveTo (x, y) performs the same actions.
An exception to the general construction formula are functions for drawing rectangles (strokeRect (x, y, width, height) and fillRect (x, y, width, height)), which begin with moveTo (x, y) and end with a stroke or fill. In this case, all the image functions that are needed are combined. Each time the form is initialized, Canvas opens the data structure in the form of a list, which is filled with an instruction when stroke () or fill () is executed.
Drawing linear gradients
Canvas JavaScript create has a function: createLinearGradient (x_ini, y_ini, x_fin and and_fin). It gives a linear gradient using the line defined by the starting point (x_ini, y_ini) and the ending (x_fin, y_fin). The direction of this straight line shows the movement of the gradient. For example, if you want the gradient to go in the vertical direction, create a vertical line. She defines his first color. Length indicates the intensity of the gradient. If programs like Photoshop are used, the operation will be similar. To finish the definition of the gradient, indicate the colors that form it, using the function: addColorStop (position, color).
The position parameter determines where the color specified by the second parameter is applied from. Position is expressed as a number from zero to one. At least two different colors are needed to form the gradient. For example, if you want it to start in red and end in white:
- addColorStop (0, “red”);
- addColorStop (1, "white").
To draw radial gradients, Canvas has a function: createRadialGradient (x1, y1, r1, x2, y2, r2).
It defines a radial gradient based on two circles, each of which with the position of its center (x, y) and radius (r). If the center of both circles is the same, then the effect will be uniform, while in another case, a sphere effect will be created, depending on the radius.
Work with images
Canvas can work with images in the most common formats (GIF, JPEG, PNG) and has functions for control - size and rotation at the pixel level. This gives great opportunities for processing drawings using canvas. You can use the image as a background, make sketches, zoom and change the color.
The first step in working with the canvas is to upload a photo. There are several ways to do this. Until it is fully loaded, the user will not be able to work with him. The two most common download methods:
- An object inside Javascript. Using a new image, create an instance of the Image object, then give it a name and start using it after loading.
- The image of the web page. Using a tag, a picture is downloaded from the body of the site and assigned an identifier, for example, a name, to access it from the document.getElementById used.
If you want it to be visible only after processing, use the hidden property in the tag. There are other loading methods, for example, using the created image in another object. It can also be downloaded using the data: url method. When the download is complete, you can draw using drawImage, which supports several formats: drawImage (image, x, y). Draw an image on the canvas by placing the upper left corner in the coordinates (x, y).
JavaScript Canvas drawimage (image, x, y, width, height) allows you to scale the picture. The first three parameters have the same meaning as in the previous case. Width and height determine the size (in pixels) in which graphics will be displayed. If the layout is larger than the original, they get an increase in the image, and if it is smaller, they will be miniaturized. If you want the image to not deform, you must maintain the ratio of the width and height of the original image.
DrawImage (image, x1, y1, width1, height1, x2, y2, width2, height2) scales part of the picture. With this function, only the contents of the colored frame will be displayed on the canvas, it was included as a background. This function takes the part of the original image defined as x1, y1 (width 1, height 1) and draws this part at a different position (x2, y2) and at a different scale (width 2, height 2). In this case, an increase in the region is created because the values of width and height 2 are greater than 1.
ClearRect example
ClearRect draws a transparent rectangle on the canvas. Unlike the rect, strokeRect, or fillRect functions that represent a colored shape. This should not be confused with drawing a white rectangle.
The background image is set by CSS, a black rectangle is displayed on the canvas. If you close it with a white background, the transparent one will be hidden. Instead, it’s better to use the clearRect function to draw the JavaScript Canvas “clean” rectangle over the entire canvas, thus allowing the artwork to shine through.
You can also clean the canvas by setting the width or height - just set them on the canvas, assigning it a new value:
- // canvas.width = 600;
- canvas.width = canvas.width.
This resets the transformation matrix.
Canvas JavaScript clearrect clears all the pixels on the canvas in the given rectangle (x, y, w, h) to transparent black. The clearCanvas () function can be used to fine-tune the drawing and canvas text. This feature is useful when you need to remove the entire context in order to add new pictures to an empty layout. He must clean it every time the mouse moves and a new line is drawn.
When the “Set Number” button is clicked, a random number from 1 to 100 is displayed on the canvas. The “Clear” button removes everything from the context so as not to display numbers one above the other. This will happen if the “Set number” button is pressed again.
The syntax in this case is: context.clearRect (x, y, w, h).
This is sample code for drawing a filled rectangle in Canvas JavaScript using fillRect, which needs to clear the center. FillRect applies the width and height of the canvas, and clearRect uses percentages of these values to create the frame. This example shows only the Canvas JavaScript clear method. X, y, width and height of the cleared rectangle are shown as a percentage.
Cleaning sequence:
- Load the canvas and draw lines on it.
- Call clearRect ().
- Resize by manipulating the width and height of the canvas and div elements with a 1: 1 pixel ratio.
User reviews
Canvas handles the integration of many third-party vendors such as Flipgrid, Padlet, and Storyline. Therefore, it is very convenient for the designer.
In reviews, users of the functionality of the service for working with Canvas JavaScript report the following:
- It uses a simple Rich Text Editor, which is easy to create text, insert pictures and videos, improve text with an HTML editor. This gives the designer the ability to create more than just text on the page.
- Works great with third-party vendors by integrating them directly into Canvas. This feature allows you to use additional tools such as McGraw-Hill Connect, Kaltura, Box, Office 365, Google Drive, and many others.
- The canvas has a cascading approach to organizational design at the root account level or as part of any additional. Each subaccount inherits the attributes of its "parents". Managing all of these elements has become incredibly easy thanks to the SIS import of .csv files. Instructure constantly updates service documentation.
- Canvas is a learning management system that can be used by a small organization offering just a couple of lessons locally, or a giant organization offering thousands of courses for hundreds of thousands of students around the world.
- The scalability of a Canvas instance is very powerful. The service is intuitive and highly customizable.
- Provides a flexible platform where the user can share experiences with others. Suitable for integration with many useful services such as Drive. This is quite convenient, since you can use only those functions that are needed.
- Integration with Drive and Gmail may be more intuitive.
- Canvas has great customer service. Added new products and improvements.
- The canvas is very flexible, allows you to duplicate individual tasks.
- Canvas integrates well with Crocodoc for online commenting and evaluation of work, has excellent import features.
One of the important features that users would like to see is the ability to annotate PDFs or other documents on the Canvas web platform. There is currently no such feature in the web version.