Home Web Front-end H5 Tutorial HTML5每日一练之Canvas标签的应用-绘制坐标变换图形

HTML5每日一练之Canvas标签的应用-绘制坐标变换图形

May 17, 2016 am 09:09 AM

       绘制图形的时候,我们可能经常会想到旋转图形,或者对图形使用变形处理,使用Canvas API的坐标轴变换处理功能,就能实现这种效果。

       在计算机上绘制图形的时候,是以坐标点为基准来进行绘制的,默认情况下Canvas画布的最左上角对应于坐标轴的原点(0, 0)。前面我们所讲的所有利用Canvas API绘制出来的图形都是以画布最左上交为坐标轴圆点,并以像素为单位来进行绘制的。

      如果对这个坐标轴进行改变,那么就可以实现图形的变换处理了。对坐标的变换处理有以下三种方式:


平移
使用图形上下文对象的translate方法移动坐标轴原点,该方法定义如下:

  • cantext.translate(x, y);
    Copy after login



x:表示横坐标,也就是将坐标轴x从原点向【左】移动多少个单位,默认以像素为单位
y:表示纵坐标,也就是将坐标轴y从原点向【下】移动多少个单位,默认以像素为单位


扩大
使用图形上下文对象的scale方法将图形放大,该方法的定义如下所示:

  • cantext.scale(x, y);
    Copy after login



x:表示横坐标,也就是【水平方向】将图形放大的倍数
y:表示纵坐标,也就是【垂直方向】将图形放大的倍数
注:将图形缩小的时候,将这两个参数设置为0-1之间的小数就可以了,比如,0.5表示将图形缩小一半。


旋转
使用图形上下文对象的rotate方法将图形进行旋转,该方法的定义如下所示:

cantext.rotate(angle);
Copy after login
    angle:
  • 是指旋转的角度,旋转的中心点是坐标轴的原点。旋转方向为顺时针进行,要想逆方旋转,只需要设置为负数即可。

  • 下面的案例,就是使用这三种坐标变换方式共同实现的下图效果:

    930.png
<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
        <title>HTML5每日一练之Canvas标签的应用-绘制坐标变换图形</title>
                <script type="text/javascript">
                window.onload = function()
                 {
                        var canvas = document.getElementById("W3Cfuns_canvas");
                        var context = canvas.getContext("2d");
                        context.fillStyle = "#d4d4d4";
                        context.fillRect(0, 0, 400, 300);
                        //绘制图形
                        context.translate(200, 25);
                        context.fillStyle = "rgba(0, 0, 255, 0.25)";
                        for(var i = 0; i < 50; i++)
                        {
                                context.translate(25, 25);
                                context.scale(0.95, 0.95);
                                context.rotate(Math.PI / 10);
                                context.fillRect(0, 0, 100, 50);
                        }
                }
        </script>
    </head>
    
    <body>
            <canvas id="W3Cfuns_canvas" width="600" height="400"></canvas>
    </body>
</html>
Copy after login

以上就是HTML5每日一练之Canvas标签的应用-绘制坐标变换图形的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles