一、绘制基本图形
在每次用canvas画布时,都有几步是“套路”
1.在HTML中创建Canvas画布:
<span style="color: #0000ff;"><span style="color: #800000;">canvas </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="mycanvas"</span><span style="color: #ff0000;"> width</span><span style="color: #0000ff;">="960px"</span><span style="color: #ff0000;"> height</span><span style="color: #0000ff;">="580px"</span><span style="color: #0000ff;">></span><span style="color: #000000;"> 浏览器不支持canvas </span><span style="color: #008000;"><!--</span><span style="color: #008000;"> 如果不支持会显示这段文字 </span><span style="color: #008000;">--></span> <span style="color: #0000ff;"></span><span style="color: #800000;">canvas</span><span style="color: #0000ff;">></span></span>
2.获取画布标签,并得到一个2D对象:
<span style="color: #0000ff;">var</span> c = document.getElementById('mycanvas'); <span style="color: #008000;">//</span><span style="color: #008000;">获取标签</span> <span style="color: #0000ff;">var</span> ctx = c.getContext("2d"); <span style="color: #008000;">//</span><span style="color: #008000;">返回一个用来绘制环境类型的环境,返回一个2D对象 ,该对象实现了一个画布使用的大多数方法。</span>
1、绘制线条:
ctx.lineWidth = 10; <span style="color: #008000;">//</span><span style="color: #008000;">设置线条宽度 </span> ctx.strokeStyle = "red"; <span style="color: #008000;">//</span><span style="color: #008000;">设置线条的颜色</span> ctx.beginPath(); <span style="color: #008000;">//</span><span style="color: #008000;">创建一个新的路径</span> ctx.moveTo(10,10); <span style="color: #008000;">//</span><span style="color: #008000;">将画笔光标位置移动到坐标(10,10)处</span> ctx.lineTo(150,10); <span style="color: #008000;">//</span><span style="color: #008000;">移动画笔到坐标(150,50)处</span> ctx.stroke(); <span style="color: #008000;">//</span><span style="color: #008000;">开始绘制定义好的路径</span>
通过ctx.lineCap="butt"可以设置线条的形状。有三个可取值:butt、round、square。
2、绘制空心矩形:
md5.strokeRect(100,100,200,240); <span style="color: #008000;">//</span><span style="color: #008000;">绘制空心矩形的函数,四个参数分别表示起点X、Y坐标、矩形长、矩形宽</span>
3、绘制实心矩形:
方法一:使用fillRext()函数。在绘制实心矩形时,可使用fillStyle设置图形的颜色。
方法二:
md5.rect(10,10,70,40<span style="color: #000000;">); md5.fill();</span>
4、画圆:
使用arc()函数可以画一个圆弧。有六个参数:圆弧中心X、Y坐标、圆弧半径、起始角度、终
止角度、是否逆时针。可以调用fill()函数画一个实心圆。
5、画圆角矩形:
用arcTo函数可以画圆角矩形。有五个参数:P1的X、Y和P2的X、Y、圆弧的半径radius。
6、擦除Canvas画板:
clearRect()函数。四个参数:X、Y、长度、宽度。
7、绘制复杂:二维贝塞尔曲线、三维贝塞尔曲线。
8、利用clip()函数可在指定区域绘图。
9、绘制自定义图形。
二、绘制文本
1、cxt.font:设置字体样式。
ctx.font = "30px Arial"; <span style="color: #008000;">//</span><span style="color: #008000;">设置文字大小和字体样式、字体大小、斜体效果</span>
2、cxt.strokeText():设置文字内容。空心文字!有四个参数:文本字符串,坐标X、坐标Y、文本宽 。最后一个参数可以省去,省去时文本宽度自动设定为整个文本的宽度。
ctx.strokeText("Hello World",100,50); <span style="color: #008000;">//</span><span style="color: #008000;">设置文字内容</span>
3、cxt.fillText():设置文字内容。实心文字!参数同strokeText();
4、ctx.textAlign=" ";设置文字对齐方式:center、left、right。
5、ctx.textBaseline=" "; 设置文字纵向对齐方式:top、middle、bottom、ideographic等。
三、图片操作
1、利用drawImage()函数绘制图片,该函数有三种原型:
<span style="color: #000000;">drawImage(image,dx,dy); drawImage(image,dx,dy,dw,dh); drawImage(image,sx,sy,sw,sh,dx,dydw,dh);</span>
第一参数都是要绘制的对象。
绘制图像方法一:
在HTML添加如下代码:
<span style="color: #0000ff;"><span style="color: #800000;">img </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="face"</span><span style="color: #ff0000;"> src</span><span style="color: #0000ff;">="1.jpg"</span><span style="color: #ff0000;"> alt</span><span style="color: #0000ff;">="the face"</span><span style="color: #ff0000;"> width</span><span style="color: #0000ff;">="240px"</span><span style="color: #ff0000;"> height</span><span style="color: #0000ff;">="240px"</span><span style="color: #0000ff;">></span></span>
JavaScript代码如下:
<span style="color: #0000ff;">var</span> img=document.getElementById("face"<span style="color: #000000;">); image.onload </span>=<span style="color: #0000ff;">function</span><span style="color: #000000;">(){ ctx.drawImage(image,</span>10,10<span style="color: #000000;">); } </span>
绘制图像方法二:
<span style="color: #0000ff;">var</span> image = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Image(); image.src </span>= "1.jpg"<span style="color: #000000;">; image.onload </span>=<span style="color: #0000ff;">function</span><span style="color: #000000;">(){ ctx.drawImage(image,</span>10,10<span style="color: #000000;">); ctx.drawImage(image,</span>260,10,100,100<span style="color: #000000;">); ctx.drawImage(image,</span>50,50,100,100,260,130,100,100<span style="color: #000000;">); } </span>
一定要添加图片的onload事件监听!
第一个函数从(10,10)开始绘制整张图片。
第二个函数从(260,10)绘制整张图片到长100、宽100的矩形区域内。
第三个函数表示截取图片从(50,50)到(100,100)的部分,从坐标(260,130)开始绘制,放到长100、宽100的矩形区域内。
2、利用getImageData和putImageData绘制图片。
3、利用createImageData新建像素。

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

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),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment