Table of Contents
The first step: pick up the brush and click on the country
The second step: pick up the pen and draw the country
Notes about the canvas
Home Web Front-end H5 Tutorial Analysis summary of beginPath() and closePath() in Canvas (with examples)

Analysis summary of beginPath() and closePath() in Canvas (with examples)

Oct 26, 2018 pm 02:59 PM
canvas

This article brings you an analysis and summary of beginPath() and closePath() in Canvas (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. help.

While learning the new elements of H5, I learned about canvas: you can draw the required graphics on the web page; when drawing the fan chart, I discovered problems with beginPath() and closePath(). , will be analyzed and summarized next;

The first step: pick up the brush and click on the country

<!--html代码-->
<canvas id="canvas4" width="400px" height="300px"></canvas>
Copy after login
Copy after login
<script>
    var canvas4= document.getElementById("canvas4");
    var content4 = canvas4.getContext("2d");
   
    content4.moveTo(200,150);
    content4.arc(200, 150, 100, 0, Math.PI * 0.3);
    content4.fillStyle = "black";
    content4.fill();
    
    content4.moveTo(200,150);
    content4.arc(200, 150, 100,Math.PI * 0.3,Math.PI * 0.7);
    content4.fillStyle = "yellow";
    content4.fill();
</script>
Copy after login

The display effect is as shown in the figure:
Analysis summary of beginPath() and closePath() in Canvas (with examples)

Analysis:
It can be seen from the display effect that it does not display the desired effect at all; the first block should be black and the second block should be yellow.
At this point we need to mention the important roles of beginPath and closePath;
Don’t worry, let’s analyze step by step why the above two methods are needed:
1: moveTo(x,y) means moving the brush to (x , y) position, which is also the position that defines the start of the line. If there is no moveTo method, then it is not even a fan shape, it is directly a small crescent;
The display effect is as shown in the figure:
Analysis summary of beginPath() and closePath() in Canvas (with examples)

2: When using canvas to draw, the brush starts drawing from beginPath() every time. If it cannot be found at the current starting point, continue to search upward until it is found, and then starts after beginPath(). Drawing, so it will appear that the previous sector is covered by the latter sector, leaving only a black edge struggling -_-
3: fillRect() and storkeRect() draw independent areas method cannot interrupt the current path, that is to say, it cannot replace the function of closePath() [close path];
4: beginPath() and closePath() must appear in pairs! Because if you want to start a new path by closing a path, the starting path will not be a new path.

The second step: pick up the pen and draw the country

Use points as surfaces, supplement them, and accumulate them into paintings;

<!--html代码-->
<canvas id="canvas4" width="400px" height="300px"></canvas>
Copy after login
Copy after login
rrree

The normal display effect is as shown in the figure:

Analysis summary of beginPath() and closePath() in Canvas (with examples)

Notes about the canvas

1: The default width and height of the canvas is 300 * 150;
2: If you want to set the width and height of the canvas, you must use Set the inline attributes instead of style; otherwise, even if the size of the canvas is visually changed, it will not actually change, and the content inside will be deformed;
3: Be sure to do this before drawing on the canvas First create the context object, and then use the context object to call the properties and methods of the canvas for manipulation;
4: There is no method or attribute for drawing circles in canvas, but there is a method for drawing arcs, arc(), which can be used To draw a 2PI sector or circle;
5: beginPath() and closePath() are very important. If the display in your canvas does not look correct, please first check whether beginPath() and closePath are used correctly. ();

The above is the detailed content of Analysis summary of beginPath() and closePath() in Canvas (with examples). For more information, please follow other related articles on the PHP Chinese website!

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)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1263
29
C# Tutorial
1236
24
Which schools use canvas? Which schools use canvas? Aug 18, 2023 pm 05:59 PM

Schools using canvas include Stanford University, MIT, Columbia University, University of California, Berkeley, etc. Detailed introduction: 1. Stanford University uses Canvas as its main online learning platform. Teachers and students at Stanford University use Canvas to manage and communicate course content, and learn through functions such as online discussions, assignment submissions, and exams; 2. Ma Provincial Polytechnic Institute and MIT also use Canvas as their online learning management system and conduct course management through the Canvas platform; 3. Columbia University, etc.

Learn the canvas framework and explain the commonly used canvas framework in detail Learn the canvas framework and explain the commonly used canvas framework in detail Jan 17, 2024 am 11:03 AM

Explore the Canvas framework: To understand what are the commonly used Canvas frameworks, specific code examples are required. Introduction: Canvas is a drawing API provided in HTML5, through which we can achieve rich graphics and animation effects. In order to improve the efficiency and convenience of drawing, many developers have developed different Canvas frameworks. This article will introduce some commonly used Canvas frameworks and provide specific code examples to help readers gain a deeper understanding of how to use these frameworks. 1. EaselJS framework Ea

What versions of html2canvas are there? What versions of html2canvas are there? Aug 22, 2023 pm 05:58 PM

The versions of html2canvas include html2canvas v0.x, html2canvas v1.x, etc. Detailed introduction: 1. html2canvas v0.x, which is an early version of html2canvas. The latest stable version is v0.5.0-alpha1. It is a mature version that has been widely used and verified in many projects; 2. html2canvas v1.x, this is a new version of html2canvas.

uniapp implements how to use canvas to draw charts and animation effects uniapp implements how to use canvas to draw charts and animation effects Oct 18, 2023 am 10:42 AM

How to use canvas to draw charts and animation effects in uniapp requires specific code examples 1. Introduction With the popularity of mobile devices, more and more applications need to display various charts and animation effects on the mobile terminal. As a cross-platform development framework based on Vue.js, uniapp provides the ability to use canvas to draw charts and animation effects. This article will introduce how uniapp uses canvas to achieve chart and animation effects, and give specific code examples. 2. canvas

What are the canvas arrow plug-ins? What are the canvas arrow plug-ins? Aug 21, 2023 pm 02:14 PM

The canvas arrow plug-ins include: 1. Fabric.js, which has a simple and easy-to-use API and can create custom arrow effects; 2. Konva.js, which provides the function of drawing arrows and can create various arrow styles; 3. Pixi.js , which provides rich graphics processing functions and can achieve various arrow effects; 4. Two.js, which can easily create and control arrow styles and animations; 5. Arrow.js, which can create various arrow effects; 6. Rough .js, you can create hand-drawn arrows, etc.

What are the details of the canvas clock? What are the details of the canvas clock? Aug 21, 2023 pm 05:07 PM

The details of the canvas clock include clock appearance, tick marks, digital clock, hour, minute and second hands, center point, animation effects, other styles, etc. Detailed introduction: 1. Clock appearance, you can use Canvas to draw a circular dial as the appearance of the clock, and you can set the size, color, border and other styles of the dial; 2. Scale lines, draw scale lines on the dial to represent hours or minutes. Position; 3. Digital clock, you can draw a digital clock on the dial to indicate the current hour and minute; 4. Hour hand, minute hand, second hand, etc.

What properties does tkinter canvas have? What properties does tkinter canvas have? Aug 21, 2023 pm 05:46 PM

The tkinter canvas attributes include bg, bd, relief, width, height, cursor, highlightbackground, highlightcolor, highlightthickness, insertbackground, insertwidth, selectbackground, selectforeground, xscrollcommand attributes, etc. Detailed introduction

Explore the powerful role and application of canvas in game development Explore the powerful role and application of canvas in game development Jan 17, 2024 am 11:00 AM

Understand the power and application of canvas in game development Overview: With the rapid development of Internet technology, web games are becoming more and more popular among players. As an important part of web game development, canvas technology has gradually emerged in game development, showing its powerful power and application. This article will introduce the potential of canvas in game development and demonstrate its application through specific code examples. 1. Introduction to canvas technology Canvas is a new element in HTML5, which allows us to use

See all articles