English 中文(简体)
What is the HTML5 canvas?
原标题:

I m a little confused to what HTML5 canvas is. I ve been told it is JavaScript, but it seems to be a much bigger deal?

  1. What makes it different than javascript?

  2. Why is it so amazing?

  3. Does it do other things than JavaScript?

最佳回答

I suggest you read this article HTML5 Canvas - the basics

But in short. It does not replace javascript.

HTML 5 canvas gives you an easy and powerful way to draw graphics using JavaScript. For each canvas element you can use a "context" (think about a page in a drawing pad), into which you can issue JavaScript commands to draw anything you want. Browsers can implement multiple canvas contexts and the different APIs provide the drawing functionality.

问题回答

The canvas is basically an img element that you can draw on using javascript.

The Canvas element is essentially a drawing canvas that can be painted on programmatically; a sort of scriptable bitmap drawing tool for the web.

I suppose the "amazing" thing about it, apart from the fact that we can now all create web-based MS Paint clones with ease, is that you have a much richer, completely free-form area for creating complex graphics client-side and on-the-fly. You can draw pretty graphs, or do things with photos. Allegedly, you can also do animation!

Mozilla s Developer Center has a reasonable tutorial if you want to try it out.

First of all, Canvas is NOT JavaScript! These 2 are totally different things.

Canvas is a HTML5 element that can be used for rendering graphics, animation, graphs, photo compositions or any other visual objects on the fly by using JavaScript. More often, canvas has used for building web game and online presentation.

  • Canvas - A rectangle area like white paper
  • Context - Returns the object by using what we can call many functions on which is used to draw the graphics and animations on a canvas (like how pencils are used on paper)

See the following example which draws a line on the canvas:

<html>
      <body>
       <canvas id="c" width="200" height="200" style="border:1px solid"></canvas>
        <script>
          var canvas = document.getElementById("c");//get the canvas in javascript
          var context = canvas.getContext("2d");//getcontext on canvas
          context.beginPath();//start the path.we are going to draw the line
          context.moveTo(20,20);//starting point of Line
          context.lineTo(40,20);//ending point of Line
          context.stroke(); //ink used for drawing Line (Default: Black)
        </script>
      </body>
    </html>




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签