如何使用HTML5创建一个变换矩阵?

如何使用html5创建一个变换矩阵?

In the following article, we are going to learn about how to create a transformation matrix with HTML5. HTML5 canvas provides methods that allow modifications directly to the transformation matrix. The transformation matrix must initially be the identity transform. It may then be adjusted using the transformation methods.

Using the transform() method

The transformation matrix of the current context can be changed using the transform() method.in other words,transform() method lets you scale, rotate, move, and skew the current context.

注意− 只有在调用transform()方法之后创建的图形才会受到变换的影响。

语法

以下是transform()方法的语法

立即学习“前端免费学习笔记(深入)”;

ctx.transform(m11, m12, m21, m22, dx, dy)

登录后复制

使用set transform()方法

The transform(a1, b1, c1, d1, e1, f1) function is then called with the same arguments after the setTransform(a1, b1, c1, d1, e1, f1) method resets the current transform to the identity matrix.

语法

Following is the syntax for set transform() method.

ctx.setTransform(m11, m12, m21, m22, dx, dy)

登录后复制

让我们来看一些示例,以更好地理解HTML5中的变换矩阵

Example 1

在以下示例中,我们使用transform()方法生成一个矩形。

         var canvas = document.getElementById("tutorial");      if (canvas.getContext){         var ctx = canvas.getContext('2d');         ctx.beginPath();         ctx.lineWidth = "4";                  var cos=Math.cos(45*Math.PI / 180);         var sin=Math.cos(45*Math.PI / 180);                  ctx.transform(cos, sin, -sin, cos, 160, 20);         ctx.strokeStyle = "green";         ctx.strokeRect(60, 60, 160, 160);         ctx.stroke();      }   

登录后复制

当脚本被执行时,它将通过触发变换方法,在网页上生成一个显示矩形的输出。

Example 2

在下面的示例中,我们使用了transform()和set transform()方法。

         function drawShape(){         // get the canvas element using the DOM         var canvas = document.getElementById('mycanvas');         // Make sure we don't execute when canvas isn't supported         if (canvas.getContext){            // use getContext to use the canvas for drawing            var ctx = canvas.getContext('2d');            var sin = Math.sin(Math.PI/6);            var cos = Math.cos(Math.PI/6);            ctx.translate(200, 200);            var c = 0;            for (var i=0; i <= 12; i++) {               c = Math.floor(255 / 12 * i);               ctx.fillStyle = "rgb(" + c + "," + c + "," + c + ")";               ctx.fillRect(0, 0, 100, 100);               ctx.transform(cos, sin, -sin, cos, 0, 0);            }            ctx.setTransform(-1, 0, 0, 1, 200, 200);            ctx.fillStyle = "rgba(100, 100, 255, 0.5)";            ctx.fillRect(50, 50, 100, 100);         }          else {            alert('You need Safari or Firefox 1.5+ to see this demo.');         }      }   

登录后复制

On running the above script, it will generate an output generated by using transform() and set transform() methods on the webpage.

Example 3

在下面的示例中,我们正在创建一个数学表达式 Σ n = 1。

         function tutorial() {         const ctx = document.getElementById('mytutorial').getContext('2d');         const sin = Math.sin(Math.PI / 6);         const cos = Math.cos(Math.PI / 6);         ctx.translate(100, 100);         let c = 0;         for (let i = 0; i <= 10; i++) {            c = Math.floor(255 / 12 * i);            ctx.fillStyle = `rgb(88, 214, 141 )`;            ctx.fillRect(0, 0, 100, 10);            ctx.transform(cos, sin, -sin, cos, 0, 0);         }         ctx.setTransform(-1, 0, 0, 1, 100, 100);         ctx.fillStyle = 'rgb(211, 84, 0,0.5 )';         ctx.fillRect(0, 50, 100, 100);      }   

登录后复制

当用户尝试执行该脚本时,它将通过在网页上使用transform()和set transform()方法生成的输出进行显示。

以上就是如何使用HTML5创建一个变换矩阵?的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2823067.html

(0)
上一篇 2025年3月9日 01:54:00
下一篇 2025年2月25日 23:40:20

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • HTML5中的Canvas免费库有哪些?

    使用脚本,canvas元素用于立即创建图形(通常是JavaScript)。该标签是 HTML5 中刚刚引入的。 元素仅用作图像的保存区域。要绘制视觉效果,您必须使用脚本。有多种方法可以在画布上绘制路径、方框、圆圈、字符以及添加图像。 语法 …

    2025年3月9日
    200
  • 在HTML5中,当触发上下文菜单时执行脚本?

    使用 HTML5 中的 contextmenu 属性在上下文菜单打开时执行脚本。当用户右键单击时会生成上下文菜单。 示例 您可以尝试运行以下代码来实现contextmenu属性 –          HTML menuitem …

    2025年3月9日
    200
  • html的注释符号是哪个

    html的注释符号是“”,注释符号之间的内容将被视为注释,不会被浏览器解析和显示,注释可以包含任何文本,包括说明、备注、代码片段等,注释可以用于添加注释、临时禁用代码、添加调试信息和解决浏览器兼容性问题,以便开发人员可以更好地理解和管理代码…

    2025年3月9日
    200
  • html搜索框代码怎样写

    html搜索框代码编写步骤:1、使用标签来创建HTML表单元素来包含搜索框;2、在表单中使用标签来创建输入框,同时设置type属性为”text”,用于用户输入搜索关键字;3、使用标签创建按钮,同时设置type属性为&…

    2025年3月9日
    200
  • 我如何裁剪HTML中的IFrame?

    内联框架在 HTML 中称为 iframe。 标签指定内容中的一个矩形区域,浏览器可以在其中显示带有滚动条和边框的不同文档。要在当前 HTML 文档中嵌入另一个文档,请使用内联框架。 可以使用 HTML iframe 名称属性指定 元素的引…

    2025年3月9日
    200
  • 如何在HTML5中创建定义列表?

    描述列表(以前称为定义列表)提供术语及其定义的有组织的排列。 定义列表的标签如下所示:− − 这是定义列表。它将术语及其定义存储为行和数据的表格。 – 这是用于定义的术语。它使短语保持定义。 立即学习“前端免费学习笔记(深入)”…

    2025年3月9日
    200
  • 如何在HTML/CSS中设置复选框的大小?

    HTML(即超文本标记语言)用于创建网页并定义其结构和内容,CSS 能够对其进行样式设置。 HTML 具有用于创建网页(包括 Web 表单)的多种元素。 复选框是Web表单和用户界面的重要组成部分。当需要选择多个选项时,使用复选框。默认情况…

    2025年3月9日
    200
  • 如何在HTML中允许跨域使用图像和画布?

    为了允许跨域使用图像和画布,服务器必须在其HTTP响应中包含适当的CORS(跨域资源共享)头。这些头可以设置为允许特定的来源或方法,或者允许任何来源访问资源。 HTML Canvas An HTML5 Canvas is a rectang…

    2025年3月9日
    200
  • html怎么转txt

    html转txt的方法有使用文本编辑器、使用在线转换工具和使用Python编程。详细介绍:1、打开HTML文件,可以使用任何文本编辑器,如记事本、Sublime Text等,选择整个HTML文件的内容,可以通过按Ctrl+A快捷键或通过鼠标…

    2025年3月9日
    200
  • 如何在HTML表单中添加颜色选择器?

    HTML,代表超文本标记语言,是超文本和标记语言的组合,我们可以用它来构建网页及其内容。本文将展示如何使用 HTML 添加颜色选择器。 语法 For Output Code pre class 登录后复制 标签是 HTML 中的交互式元素,…

    2025年3月9日
    200

发表回复

登录后才能评论