iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > VUE >HTML5 Canvas有什么用
  • 854
分享到

HTML5 Canvas有什么用

2024-04-02 19:04:59 854人浏览 泡泡鱼
摘要

这篇文章给大家分享的是有关HTML5 canvas有什么用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。<canvas>是一个新的html元素,这个元素可以被Scri

这篇文章给大家分享的是有关HTML5 canvas有什么用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

<canvas>是一个新的html元素,这个元素可以被Script语言(通常是javascript)用来绘制图形。例如可以用它来画图、合成图象、或做简单的(和不那么简单的)动画。右面的图象展示了一些<canvas>的应用示例,我们将会在此教程中看到他们的实现。

<canvas>最先在苹果公司(Apple)的Mac OS X Dashboard上被引入,而后被应用于Safari。基于Gecko1.8的浏览器,例如Firefox 1.5,也支持这个新元素。元素<canvas>是WhatWG WEB applications 1.0也就是大家都知道的HTML 5标准规范的一部分。

在本教程中,我将试着讲述如何在你自己的网页中使用<canvas>元素。提供的示例应该会给你些清晰概念,即用<canvas>能做些什么的。这些示例也可作为你应用<canvas>的起点。

开始使用之前
用元素<canvas>并不难,只要你具有HTML和 JavaScript的基础知识。

如上所述,并不是所有现代浏览器都支持<canvas>元素,所以你需要 Firefox 1.5或更新版本、或者其他基于Gecko的浏览器例如Opera 9、或者最近版本的Safari才能看到所有示例的动作。

<canvas>元素

Let's start this tutorial by looking at the <canvas> element itself.
让我们从<canvas>元素的定义开始吧。

<canvas id="tutorial" width="150" height="150"></canvas>

This looks a lot like the <img> element, the only difference is that it doesn't have the src and alt attributes. <canvas>看起来很像<img>,唯一不同就是它不含 src 和 alt 属性。The <canvas> element has only two attributes - width and height. These are both optional and can also be set using DOM properties or CSS rules.它只有两个属性,widthheight,两个都是可选的,并且都可以 DOM 或者 CSS 来设置。 When no width and height attributes are specified, the canvas will initially be 300 pixels wide and 150 pixels high.如果不指定width 和 height,默认的是宽300像素高150像素。The element can be sized arbitrarily by CSS, but during rendering the image is scaled to fit its layout size.   (If your  renderings seem distorted, try specifying your width and height attributes explicitly in the <canvas> attributes, and not with CSS.)虽然可以通过 CSS 来调整canvas的大小,但渲染图像会缩放来适应布局的(如果你发现渲染结果看上去变形了,不必一味依赖CSS,可以尝试显式指定canvas的width 和 height 属性值)。

The id attribute isn't specific to the <canvas> element but is one of default HTML attributes which can be applied to (almost) every HTML element (like class for instance). It's always a Good idea to supply an id because this makes it much easier to identify it in our script.
id  属性不是<canvas>专享的,就像标准的HTLM标签一样,任何一个HTML元素都可以指定其 id 值。一般,为元素指定 id 是个不错的主意,这样使得在脚本中应用更加方便。

The <canvas> element can be styled just like any nORMal image (margin, border, background, etc). These rules however don't affect the actual drawing on the canvas. We'll see how this is done later in this tutorial. When no styling rules are applied to the canvas it will initially be fully transparent. <canvas>元素可以像普通图片一样指定其样式(边距,边框,背景等等)。然而这些样式并不会对canvas实际生成的图像产生什么影响。下面我们会看到如何应用样式。如果不指定样式,canvas默认是全透明的。

替用内容

Because the <canvas> element is still relatively new and isn't implemented in some browsers (such as Firefox 1.0 and Internet Explorer), we need a means of providing fallback content when a browser doesn't support the element.

因为 <canvas> 相对较新,有些浏览器并没实现,如Firefox 1.0 和 Internet Explorer,所以我们需要为那些不支持canvas的浏览器提供替用显示内容。

Luckily this is very straightforward: we just provide alternative content inside the canvas element. Browsers who don't support it will ignore the element completely and render the fallback content, others will just render the canvas normally.
For instance we could provide a text description of the canvas content or provide a static image of the dynamically rendered content. This can look something like this:

我们只需要直接在canvas元素内插入替用内容即可。不支持canvas的浏览器会忽略canvas元素而直接渲染替用内容,而支持的浏览器则会正常地渲染canvas。例如,我们可以把一些文字或图片填入canvas内,作为替用内容:

<canvas id="stockGraph" width="150" height="150">
  current stock price: $3.15 +0.15
</canvas>

<canvas id="clock" width="150" height="150">
  <img src="images/clock.png" width="150" height="150"/>
</canvas>
结束标签 </canvas> 是必须的

In the Apple Safari implementation, <canvas> is an element implemented in much the same way <img> is; it does not have an end tag. However, for <canvas> to have widespread use on the web, some facility for fallback content must be provided. Therefore, Mozilla's implementation requires an end tag (</canvas>).

在Apple Safari里,<canvas>的实现跟<img>很相似,它并不没有结束标签。然而,为了使 <canvas> 能在web的世界里广泛适用,需要给替用内容提供一个容身之所,因此,在Mozilla的实现里结束标签(</canvas>)是必须的。

If fallback content is not needed, a simple <canvas id="foo" ...></canvas> will be fully compatible with both Safari and Mozilla -- Safari will simply ignore the end tag.

如果没有替用内容,<canvas id="foo" ...></canvas> 对 Safari 和 Mozilla 是完全兼容的&mdash;&mdash; Safari 会简单地忽略结束标签。

If fallback content is desired, some CSS tricks must be employed to mask the fallback content from Safari (which should render just the canvas), and also to mask the CSS tricks themselves from IE (which should render the fallback content).

如果有替用内容,那么可以用一些 CSS 技巧来为并且仅为 Safari 隐藏替用内容,因为那些替用内容是需要在 IE 里显示但不需要在 Safari 里显示。

渲染上下文(Rendering Context)

<canvas> creates a fixed size drawing surface that exposes one or more rendering contexts, which are used to create and manipulate the content shown. We'll focus on the 2D rendering context, which is the only currently defined rendering context. In the future, other contexts may provide different types of rendering; for example, it is likely that a 3D context based on OpenGL ES will be added.

<canvas> 创建的固定尺寸的绘图画面开放了一个或多个渲染上下文(rendering context),我们可以通过它们来控制要显示的内容。我们专注于2D 渲染上,这也是目前唯一的选择,可能在将来会添加基于OpenGL ES 的 3D 上下文。

The <canvas> is initially blank, and to display something a script first needs to access the rendering context and draw on it. The canvas element has a DOM method called getContext, used to obtain the rendering context and its drawing functions. getContext() takes one parameter, the type of context.

<canvas> 初始化是空白的,要在上面用脚本画图首先需要其渲染上下文(rendering context),它可以通过 canvas 元素对象的 getContext 方法来获取,同时得到的还有一些画图用的函数。getContext() 接受一个用于描述其类型的值作为参数。

var canvas = document.getElementById('tutorial');
var ctx = canvas.getContext('2d');

In the first line we retrieve the canvas DOM node using the getElementById method. We can then access the drawing context using the getContext method.

上面第一行通过 getElementById 方法取得 canvas 对象的 DOM 节点。然后通过其 getContext 方法取得其画图操作上下文。

检查浏览器的支持

The fallback content is displayed in browsers which do not support <canvas>; scripts can also check for support when they execute. This can easily be done by testing for the getContext method. Our code snippet from above becomes something like this:

除了在那些不支持  的浏览器上显示替用内容,还可以通过脚本的方式来检查浏览器是否支持 canvas 。方法很简单,判断 getContext 是否存在即可。

var canvas = document.getElementById('tutorial');
if (canvas.getContext){
  var ctx = canvas.getContext('2d');
  // drawing code here
} else {
  // canvas-unsupported code here
}

代码模板

Here is a minimalistic template, which we'll be using as a starting point for later examples. You can download this file to work with on your system.

我们会用下面这个最简化的代码模板来(后续的示例需要用到)作为开始,你可以 下载文件 到本地备用。

<html>
  <head>
    <title>Canvas tutorial</title>
    <script type="text/javascript">
      function draw(){
        var canvas = document.getElementById('tutorial');
        if (canvas.getContext){
          var ctx = canvas.getContext('2d');
        }
      }
    </script>
    <style type="text/css">
      canvas { border: 1px solid black; }
    </style>
  </head>
  <body onload="draw();">
    <canvas id="tutorial" width="150" height="150"></canvas>
  </body>
</html>

If you look at the script you'll see I've made a function called draw, which will get executed once the page finishes loading (via the onload attribute on the body tag). This function could also have been called from a setTimeout, setInterval, or any other event handler function just as long the page has been loaded first.

细心的你会发现我准备了一个名为 draw 的函数,它会在页面装载完毕之后执行一次(通过设置 body 标签的 onload 属性),它当然也可以在 setTimeout,setInterval,或者其他事件处理函数中被调用。

一个简单的例子

To start off, here's a simple example that draws two intersecting rectangles, one of which has alpha transparency. We'll explore how this works in more detail in later examples.

作为开始,来一个简单的吧&mdash;&mdash;绘制两个交错的矩形,其中一个是有alpha透明效果。我们会在后面的示例中详细的让你了解它是如何运作的。

<html>
 <head>
  <script type="application/x-javascript">
    function draw() {
      var canvas = document.getElementById("canvas");
      if (canvas.getContext) {
        var ctx = canvas.getContext("2d");

        ctx.fillStyle = "rgb(200,0,0)";
        ctx.fillRect (10, 10, 55, 50);

        ctx.fillStyle = "rgba(0, 0, 200, 0.5)";
        ctx.fillRect (30, 30, 55, 50);
      }
    }
  </script>
 </head>
 <body onload="draw();">
   <canvas id="canvas" width="150" height="150"></canvas>
 </body>
</html>

感谢各位的阅读!关于“HTML5 Canvas有什么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

--结束END--

本文标题: HTML5 Canvas有什么用

本文链接: https://www.lsjlt.com/news/82958.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • HTML5 Canvas有什么用
    这篇文章给大家分享的是有关HTML5 Canvas有什么用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。<canvas>是一个新的HTML元素,这个元素可以被Scri...
    99+
    2024-04-02
  • HTML5 Canvas API有什么用
    这篇文章主要为大家展示了“HTML5 Canvas API有什么用”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“HTML5 Canvas API有什么用”这篇文...
    99+
    2024-04-02
  • HTML5 canvas drawImage()方法有什么用
    HTML5 canvas的drawImage()方法用于在画布上绘制图像、视频或其他画布。具体来说,drawImage()方法有三种...
    99+
    2023-10-11
    HTML5
  • HTML5 画布(Canvas)元素有什么用
    这篇文章将为大家详细讲解有关HTML5 画布(Canvas)元素有什么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。HTML5 画布(Canvas)元素 简介 HTML5 Canvas 元素是一个可编程...
    99+
    2024-04-02
  • html5中的canvas是什么
    本篇内容主要讲解“html5中的canvas是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“html5中的canvas是什么”吧! ...
    99+
    2024-04-02
  • 使用HTML5 Canvas需要注意什么
    小编给大家分享一下使用HTML5 Canvas需要注意什么,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧! 下面对我们在实际开发...
    99+
    2024-04-02
  • 怎么使用HTML5 Canvas
    这篇文章主要介绍“怎么使用HTML5 Canvas”,在日常操作中,相信很多人在怎么使用HTML5 Canvas问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么使用HTML...
    99+
    2024-04-02
  • html5中canvas怎么用
    这篇文章给大家分享的是有关html5中canvas怎么用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。大家都知道,目前并不是所有的浏览器中支持html5,就算是支持html5的浏...
    99+
    2024-04-02
  • HTML5的Canvas API怎么用
    这篇文章主要介绍“HTML5的Canvas API怎么用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“HTML5的Canvas API怎么用”文章能帮助大家解决问题...
    99+
    2024-04-02
  • html5有什么用
    html5 是一种现代标记语言,用于创建动态且交互式 web 应用程序,其主要用途包括:增强用户体验:提供离线浏览、拖放操作和原生音频/视频支持。构建复杂应用程序:支持 canvas、w...
    99+
    2024-04-05
    搜索引擎优化
  • HTML5 Canvas裁剪clip怎么用
    这篇文章主要为大家展示了“HTML5 Canvas裁剪clip怎么用”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“HTML5 Canvas裁剪clip怎么用”这...
    99+
    2024-04-02
  • 怎么用HTML5 canvas画线条
    这篇文章主要介绍“怎么用HTML5 canvas画线条”,在日常操作中,相信很多人在怎么用HTML5 canvas画线条问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么用H...
    99+
    2024-04-02
  • 怎么用HTML5 canvas画曲线
    本篇内容介绍了“怎么用HTML5 canvas画曲线”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!<...
    99+
    2024-04-02
  • canvas怎么在html5 中使用
    这期内容当中小编将会给大家带来有关canvas怎么在html5 中使用,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。<!DOCTYPE HTML><html><he...
    99+
    2023-06-09
  • HTML5中Canvas与SVG的画图原理有什么区别
    小编给大家分享一下HTML5中Canvas与SVG的画图原理有什么区别,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧! ...
    99+
    2024-04-02
  • HTML5中Canvas的使用方法有哪些
    这篇文章主要介绍了HTML5中Canvas的使用方法有哪些,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。   moveTo(x,y) 定义...
    99+
    2024-04-02
  • HTML5 canvas怎么创建
    这篇文章主要介绍“HTML5 canvas怎么创建”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“HTML5 canvas怎么创建”文章能帮助大家解决问题。 &nb...
    99+
    2024-04-02
  • 怎么用HTML5 canvas画五角星
    本篇内容介绍了“怎么用HTML5 canvas画五角星”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!<...
    99+
    2024-04-02
  • html5中canvas方法怎么使用
    这篇文章主要介绍“html5中canvas方法怎么使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“html5中canvas方法怎么使用”文章能帮助大家解决问题。 ...
    99+
    2024-04-02
  • html5的canvas元素怎么使用
    这篇文章主要介绍“html5的canvas元素怎么使用”,在日常操作中,相信很多人在html5的canvas元素怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”html...
    99+
    2024-04-02
软考高级职称资格查询
推荐阅读
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作