search
HomeWeb Front-endH5 TutorialCanvas realizes the drawing of rotating windmill

This time I will bring you the drawing of rotating windmills with Canvas. What are the precautions for drawing rotating windmills with Canvas? Here are the actual cases, let’s take a look.

Before teaching, I think if you are smart, you have mastered the basic basic operation methods of Canvas. If you don’t know much about Canvas, then I suggest you go to http://www.w3school.com.cn /tags/html_ref_canvas.asp Get familiar with it first;

okey! The picture below is the simple effect after we complete it. Action is not as good as action, so let’s do a simple drawing!

1. Define the canvas

First of all, we insert the tag into the html file to define the size of the canvas. The size of the canvas defined here is 800*600 pixels. At the same time, set the background color of the canvas in the internal style sheet (convenient for viewing when drawing);

nbsp;html>


    <meta>
    <title>Document</title>
    <style>
        body{
            padding: 0;
            margin: 0;
        }
        #canvas {
            background:#5151a2;
        }
    </style>


    <canvas></canvas>

 
The next core is to draw the windmill in the native JS environment; through JS

DOM operation method Obtain the canvas element object and obtain the 2D drawing context through getContex("2d"). This method is like telling the browser "We want to draw 2D graphics on this canvas";

<script>
    //获取画布的2d上下文
    var ctx = document.getElementById("canvas").getContext("2d");</script>

2. Draw the base of the windmill

The geometric figure of the base of the windmill looks like a long and slender trapezoid. We can draw a trapezoid and then fill it with color. Here, in order to achieve a relatively small Good effect, using the color gradient filling method; okey! Just look at the code~~!

    //定义一个函数 ,封装风车的底部基座
    function buttom(){
        ctx.beginPath();                                        //开始一条新的绘制路径
        var liner = ctx.createLinearGradient(390,600,410,600);    //设置变量(颜色渐变的方向-起点-终点)
        liner.addColorStop(0,"#ccc");                            //设置起点颜色
        liner.addColorStop(0.5,"#fff");                            //设置中点颜色
        liner.addColorStop(1,"#ccc");                            //设置终点颜色
        ctx.fillStyle = liner;                                    //梯形的填充方式设置为 变量(渐变颜色)    
        ctx.moveTo(395,300);                                    //提起我们的画笔,起点设置为(395,300)
        ctx.lineTo(405,300);                                    //连接起点画线
        ctx.lineTo(410,600);
        ctx.lineTo(390,600);                                    
        ctx.closePath();                                        //闭合路径
        ctx.fill();                                                //填充梯形
        
    }
  buttom();                              //要调用函数,才能在浏览器显示
Let’s take a look at the effect on the page. Is it very simple?

(I feel like I talk a bit too much~!~!)

3. Draw leaves

The next part will be the most critical part of this animation. First, let’s analyze the structure of the leaves. The angle between the three leaves is 120°, and the shape of each leaf is the same; they have a center, and you may also have one in your mind. I have a question, should I draw the center of the circle first or the leaves first? How should I draw the shape of the leaves? Can the leaves be copied and pasted? The answer is of course yes, Let's do it!

Idea analysis:

1). Since the shapes of the three leaves are exactly the same, we only need to draw one leaf, and just copy the second and third leaves directly. We are smart Shouldn't we encapsulate a function for this leaf drawing method? Just call it the bind( ) function! ! Just call it every time! Why! You guys are so smart

2). The three leaves have a center of a circle. In order to facilitate the coordinate values ​​when drawing the leaves, we move the center of the circle from the upper left corner of the drawing to the top of the trapezoid, so that it will be much more convenient for us to draw the leaves. ! The translate() method is used here to move the coordinate system!

3) The most difficult point is to understand how the animation is implemented here, because the animation principle will affect the document structure of our leaf drawing:

First we create a new drawing environment, we Call it environment 1. We draw the first leaf on environment 1; then rotate 120° to create the first drawing environment 2 under the premise of the first drawing environment, and then call the function bind( ) for drawing leaves. , draw two leaves; the third leaf is drawn in the same way, rotate 120° on the basis of environment 2, create a new environment 3, and call the leaf drawing function bind( ) to draw the third leaf;

If you want To implement animation, we only need to rotate the drawing environment 1 of the first leaf. The second leaf and the third leaf are drawn with reference to environment 1. Do they also move? ? Barrage: 666666

4), and finally some basic appearance style debugging! For example, color gradient, transparency, etc.!

Drawing leaves

When I drew this leaf shape, I adjusted it slowly. My aesthetics are quite low, forgive me. I can only draw such leaves. Of course, students with rich imagination can draw them according to their own preferences, but the general idea is the same;

Here I declare a variable var num = 0;, as the environment 1 rotation A parameter of degree change: Then let’s look at the code directly! ! !

var num =0;
function yezi(){
        ctx.save();  //保存默认情况下的canvas变换状态
        ctx.beginPath();
        ctx.translate(400,300);
        // ctx.globalAlpha = 0.9;
        // 设置第一次状态下 坐标系旋转度数
        ctx.rotate((Math.PI/180)*num);
        var liner1 = ctx.createLinearGradient(30,-12,30,12);      //这里设置颜色渐变填充的样式
            liner1.addColorStop(0,"#ccc");
            liner1.addColorStop(0.5,"#fff");
            liner1.addColorStop(1,"#ccc");
            ctx.fillStyle = liner1;
        ctx.save();                 //保存第一次状态  平移坐标系变换
            ctx.beginPath();
            bind();                //调用函数
            //绘制第二片叶子
            ctx.beginPath();
            ctx.rotate((Math.PI/180)*120);   //坐标系旋转120°
            ctx.save();                        //保存旋转坐标系状态,为第三片叶子做铺垫
                bind();            //调用函数
                //绘制第三片叶子
                ctx.beginPath();
                ctx.rotate((Math.PI/180)*120);   //坐标系旋转120°
                ctx.save();    
                    bind();     //调用函数
                ctx.restore();    //回复第3次状态前(旋转坐标系)
            ctx.restore();        //回复第2次状态前(旋转坐标系)
            //绘制叶子中心圆圈
            ctx.beginPath();
            var arcgradient = ctx.createRadialGradient(0,0,0,0,0,16);
            arcgradient.addColorStop(0,"#ccc");
            arcgradient.addColorStop(0.1,"#fff");
            arcgradient.addColorStop(1,"#ccc");
            ctx.arc(0,0,10,0,Math.PI*2);
            ctx.fillStyle  = arcgradient;
            ctx.fill();
        ctx.restore();             //回复第1次状态前(平移坐标系)
        num+=5;   //第一状下  环境1   态坐标系旋转度数增加********************************这个num使得环境1的旋转角度在不停的变化,**********************************************
        ctx.restore();
    }
    //绘制每片叶子都重复的代码,这里做一个函数包装
    function bind(){
        ctx.moveTo(0,0);                    
        ctx.quadraticCurveTo(10,-12,30,-12);    //比赛尔曲线
        ctx.lineTo(190,-3);
        ctx.quadraticCurveTo(200,0,190,3);    
        ctx.lineTo(30,12);
        ctx.moveTo(0,0);
        ctx.quadraticCurveTo(10,12,30,12);
        ctx.fill();
    }

4、设置动画

动画这部分就比较简单了,设置定时器,清除画布,调用函数;大功告成,打完收工!!!

setInterval(function(){
        ctx.clearRect(0,0,800,600);    //每次执行代码前,都要将画布清空,不然画出的图形会滞留在画布上;
        buttom();               //调用函数 
        yezi();
    },50);

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

H5如何做出碎片式的图片切换

怎样用H5计算手机摇动次数

H5调用相机拍照并压缩图片

The above is the detailed content of Canvas realizes the drawing of rotating windmill. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
Vue和Canvas:如何实现手写签名和手势识别功能Vue和Canvas:如何实现手写签名和手势识别功能Jul 18, 2023 am 08:49 AM

Vue和Canvas:如何实现手写签名和手势识别功能引言:手写签名和手势识别功能在现代应用程序中越来越常见,它们可以为用户提供更加直观和自然的交互方式。Vue.js作为一款流行的前端框架,搭配Canvas元素可以实现这两个功能。本文将介绍如何使用Vue.js和Canvas元素来实现手写签名和手势识别功能,并给出相应的代码示例。一、手写签名功能实现要实现手写签

Canvas的优势有哪些Canvas的优势有哪些Aug 17, 2023 pm 04:52 PM

canvas的优势有强大的绘图功能、高性能、跨平台兼容性、支持多种图形格式、可以与其他Web技术集成、可以实现动态效果和可以实现复杂的图像处理。详细介绍:1、Canvas提供了丰富的绘图功能,可以绘制各种形状、线条、文本、图像等;2、Canvas在浏览器中直接操作像素,因此具有很高的性能;3、Canvas是基于HTML5标准的一部分,可以在各种现代浏览器上运行等等。

如何利用Vue和Canvas创建逼真的天气动态背景如何利用Vue和Canvas创建逼真的天气动态背景Jul 17, 2023 am 08:33 AM

如何利用Vue和Canvas创建逼真的天气动态背景引言:在现代网页设计中,动态背景效果是吸引用户眼球的重要元素之一。本文将介绍如何利用Vue和Canvas技术来创建一个逼真的天气动态背景效果。通过代码示例,你将学习如何编写Vue组件和利用Canvas绘制不同天气场景,从而实现一个独特而吸引人的背景效果。步骤一:创建Vue项目首先,我们需要创建一个Vue项目。

canvas特效有哪些canvas特效有哪些Aug 18, 2023 pm 04:12 PM

canvas特效有粒子效果、线条动画、图片处理、文字动画、音频可视化、3D效果、游戏开发等。详细介绍:1、粒子效果,通过控制粒子的位置、速度和颜色等属性来实现各种效果,如烟花、雨滴、星空等;2、线条动画,通过在画布上绘制连续的线条,创建出各种动态的线条效果;3、图片处理,通过对图片进行处理,可以实现各种炫酷的效果,如图片切换、图片特效等;4、文字动画等等特性。

canvas插件有哪些canvas插件有哪些Aug 17, 2023 pm 05:00 PM

canvas插件有Fabric.js、EaselJS、Konva.js、Three.js、Paper.js、Chart.js和Phaser。详细介绍:1、Fabric.js 是一个基于Canvas的开源 JavaScript 库,它提供了一些强大的功能;2、EaselJS是CreateJS库中的一个模块,它提供了一套简化了Canvas编程的API;3、Konva.js等等。

Vue和Canvas:如何实现视频播放器的定制化界面Vue和Canvas:如何实现视频播放器的定制化界面Jul 18, 2023 pm 02:49 PM

Vue和Canvas:如何实现视频播放器的定制化界面引言:在现代互联网时代,视频已经成为人们生活中必不可少的一部分。为了提供良好的用户体验,许多网站和应用程序都提供了自定义的视频播放器界面。本文将介绍如何使用Vue和Canvas技术实现一个定制化的视频播放器界面。一、前期准备在开始之前,您需要确保您已经安装了Vue和Canvas,并且熟悉这两种技术的基本用法

Vue和Canvas:如何实现图片的马赛克效果Vue和Canvas:如何实现图片的马赛克效果Jul 16, 2023 pm 10:17 PM

Vue和Canvas:如何实现图片的马赛克效果引言:随着Web技术的不断发展,越来越多的人开始使用Vue框架来构建交互式的前端应用。而在前端开发中,常常需要为用户提供图片处理的功能。本文将介绍如何利用Vue和Canvas实现图片的马赛克效果,为用户带来更好的视觉体验。一、马赛克效果概述马赛克效果是一种将图像的细节部分进行像素化处理,使得整个图像变得模糊和抽象

如何使用Vue和Canvas开发网页截图工具如何使用Vue和Canvas开发网页截图工具Jul 19, 2023 am 08:36 AM

如何使用Vue和Canvas开发网页截图工具简介:随着互联网的发展,网页截图工具在我们的日常生活中扮演着越来越重要的角色。它们可以用来捕捉网页上的信息、制作教程或者分享你的见解。本文将介绍如何使用Vue和Canvas来开发一个简单的网页截图工具,以帮助读者了解如何实现这个常见但又有趣的功能。准备工作:在开始之前,我们需要准备好以下的开发环境和工具:安装Nod

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool