国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Web Front-end HTML Tutorial Getting Started with Canvas (3): Image Processing and Drawing Text_html/css_WEB-ITnose

Getting Started with Canvas (3): Image Processing and Drawing Text_html/css_WEB-ITnose

Jun 24, 2016 am 11:56 AM

Source: http://www.ido321.com/997.html

1. Image processing (unless otherwise specified, all results are from the latest version of Google)

In HTML 5, the Canvas API can not only be used to draw graphics, but can also be used to process image files on the network or disk, and then draw them in the canvas. When drawing an image, you need to use the drawImage() method:

drawImage(image,x,y): image is an image reference, x,y is the starting position in the canvas when drawing the image

drawImage(image,x,y,w,h): The first three are the same as above, w and h are the width and height of the image when drawing, which can be used to scale the image

drawImage(image,sx,sy, sw,sh,dx,dy,dw.dh): Copy all or part of the drawn image in the canvas to another location on the canvas. sx, sy, sw, sh are respectively the starting position, width and height of the copied area in the original image, and dx, dy, dw, dh represent the starting position, height and width of the copied image in the canvas.

   1: // 獲取canvas 的ID
   2: var canvas = document.getElementById('canvas');
   3: if (canvas == null)
   4: {
   5: return false;
   6: }
   7: // 獲取上下文
   8: var context = canvas.getContext('2d');
   9: context.fillStyle = '#eeeeff';
  10: context.fillRect(0,0,400,300);
  11: var image = new Image();

12: image.src = 'my.jpg';

// onload event implements loading while drawing

  13: image.onload = function()
  14: {
  15: drawImage(context,image);
  16: };
  17: function drawImage(context,image)
  18: {
  19: for (var i = 0; i < 7; i++) {
  20: context.drawImage(image,0+i*50,0+i*25,100,100);
  21: };
  22: }

Effect:

1. Image tiling

   1: // 獲取canvas 的ID
   2: var canvas = document.getElementById('canvas');
   3: if (canvas == null)
   4: {
   5: return false;
   6: }
   7: // 獲取上下文
   8: var context = canvas.getContext('2d');
   9: context.fillStyle = '#eeeeff';
  10: context.fillRect(0,0,400,300);
  11: var image = new Image();
  12: image.src = 'my.jpg';
  13: // onload事件實(shí)現(xiàn)邊繪制邊加載
  14: image.onload = function()
  15: {
  16: drawImage(canvas,context,image);
  17: };
  18: function drawImage(canvas,context,image)
  19: {
  20: // 平鋪比例
  21: var scale = 5;
  22: // 縮小圖像后寬度
  23: var n1 = image.width / scale;
  24: // 縮小圖像后高度
  25: var n2 = image.height / scale;
  26: // 橫向平鋪個(gè)數(shù)
  27: var n3 = canvas.width / n1;
  28: // 縱向平鋪個(gè)數(shù)
  29: var n4 = canvas.height / n2;
  30: for(var i = 0; i < n3; i++)
  31: {
  32: for(var j=0; j < n4; j++)
  33: {
  34: context.drawImage(image,i*n1,j*n2,n1,n2);
  35: }
  36: }
  37: }

Effect:

In HTML 5, tiling can also be achieved using context.createPattern(image,type), and the type value is the same The tiling values ??of background-image are the same.

   1: image.onload = function()
   2: {
   3: // drawImage(canvas,context,image);
   4: var ptrn = context.createPattern(image,'repeat');
   5: context.fillStyle = ptrn;
   6: context.fillRect(0,0,400,300);
   7: };

can also achieve tiling (the image is not scaled, so It is the original image size and tiled)

2. Image cropping

   1: // 獲取canvas 的ID
   2: var canvas = document.getElementById('canvas');
   3: if (canvas == null)
   4: {
   5: return false;
   6: }
   7: // 獲取上下文
   8: var context = canvas.getContext('2d');
   9: // 獲取漸變對(duì)象
  10: var g1 = context.createLinearGradient(0,400,300,0);
  11: // 添加漸變顏色
  12: g1.addColorStop(0,'rgb(255,255,0)');
  13: g1.addColorStop(1,'rgb(0,255,255)');
  14: context.fillStyle = g1;
  15: context.fillRect(0,0,400,300);
  16: var image = new Image();
  17: // onload事件實(shí)現(xiàn)邊繪制邊加載
  18: image.onload = function()
  19: {
  20: drawImage(context,image);
  21: };
  22: image.src = 'my.jpg';
  23: function drawImage(context,image)
  24: {
  25: create5StarClip(context);
  26: context.drawImage(image,-50,-150,300,300);
  27: }
  28: function create5StarClip(context)
  29: {
  30: var dx = 100;
  31: var dy = 0;
  32: var s = 150;
  33: // 創(chuàng)建路徑
  34: context.beginPath();
  35: context.translate(100,150);
  36: var x = Math.sin(0);
  37: var y = Math.cos(0);
  38: var dig = Math.PI/5 *4;
  39: // context.moveTo(dx,dy);
  40: for (var i = 0; i < 5; i++) {
  41: var x = Math.sin(i * dig);
  42: var y = Math.cos(i * dig);
  43: context.lineTo(dx+x*s,dy+y*s);
  44: }
  45: context.clip();
  46: }

Effect:

3. Pixel processing

   1: // 獲取canvas 的ID
   2: var canvas = document.getElementById('canvas');
   3: if (canvas == null)
   4: {
   5: return false;
   6: }
   7: // 獲取上下文
   8: var context = canvas.getContext('2d');
   9: var image = new Image();
  10: image.src = 'my.jpg';
  11: // onload事件實(shí)現(xiàn)邊繪制邊加載
  12: image.onload = function()
  13: {
  14: context.drawImage(image,0,0);
  15: // 獲取原圖像素
  16: var imageData = context.getImageData(0,0,image.width,image.height);
  17: for (var i = 0,n= imageData.data.length; i <n; i += 4) {
  18: // red
  19: imageData.data[i+0] = 255-imageData.data[i+0];
  20: // green
  21: imageData.data[i+1] = 255-imageData.data[i+2];
  22: // blue
  23: imageData.data[i+2] = 255-imageData.data[i+1];
  24: };
  25: // 將調(diào)整后的像素應(yīng)用到圖像
  26: context.putImageData(imageData,0,0);
  27: };

getImageData(sx,sy,sw,sh) : Indicates getting the starting coordinates and height and width of the pixel area, and returning a CanvasPixelArray object with attributes such as width, height, and data. The data attribute stores an array of pixel data, in the shape of [r1, g1, b1, a1, r2, g2, b2, a2...], r1, g1, b1, a1 are the red, green and blue values ????and transparency of the first pixel respectively, and so on.

putImageData(imagedata,dx,dy[,dirtyx,dirtyy,dirtyWidth,dirtyHeight]): Redraw pixel data onto the image. imagedata is a pixel array, dx, dy represent the starting position of redrawing, and the next four parameters give the upper left corner coordinates and height and width of a rectangle.

The pixel operation of Canvas API is only supported by some browsers, and the screenshot effect comes from the new version of Firefox

2. Draw text

   1: // 獲取canvas 的ID
   2: var canvas = document.getElementById('canvas');
   3: if (canvas == null)
   4: {
   5: return false;
   6: }
   7: // 獲取上下文
   8: var context = canvas.getContext('2d');
   9: context.fillStyle = '#00f';
  10: // 設(shè)置文字屬性
  11: context.font = 'italic 30px sans-serif';
  12: context.textBaseline = 'top';
  13: // 填充字符串
  14: context.fillText('Canvas繪制文字',0,0);
  15: context.font = 'bold 30px sans-serif';
  16: // 輪廓字符串
  17: context.strokeText('改變位置了',50,50);

fillText(string,x,y[,maxwidth]): The first three are not explained. maxwidth represents the maximum width of displayed text, which can prevent text from overflowing

strokeText(string ,x,y[,maxwidth]: Same as above.

Text attribute settings

font: Set font

textAlign: horizontal alignment, the value can be start/end/left /right/center. The default is start

textBaseline: vertical alignment, the value can be top/hanging/middle/alphabetic/ideographic/bottom. The default is alphabetic

Final effect


Next article: 9 JQuery and 5 JavaScript classic interview questions

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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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)

Explain the purpose of the role attribute in ARIA. Explain the purpose of the role attribute in ARIA. Jun 14, 2025 am 12:35 AM

ARIA's role attribute is used to define the role of web elements and improve accessibility. 1. Role attribute helps assistive technology to understand the functions of elements, such as buttons, navigation, etc. 2. Use role attributes to assign specific roles to non-semantic HTML elements. 3. The role attribute should be consistent with the element behavior and be verified by the accessibility tool test.

HTML and Design: Creating the Visual Layout of Websites HTML and Design: Creating the Visual Layout of Websites Jun 14, 2025 am 12:39 AM

How to create a website layout? 1. Use HTML tags to define the content structure, such as, ,. 2. Control styles and positions through CSS, using box model, float or Flexbox layout. 3. Optimize performance, reduce HTTP requests, use cache and optimize images, and ensure responsive design.

How can you ensure your HTML code is readable and maintainable? How can you ensure your HTML code is readable and maintainable? Jun 10, 2025 am 12:06 AM

Improve the readability and maintainability of HTML code can be achieved through the following steps: 1. Use semantic tags, such as, etc. to make the code structure clear and improve SEO effect; 2. Keep the code formatted and use consistent indentation and spaces; 3. Add appropriate comments to explain the code intention; 4. Avoid excessive nesting and simplify the structure; 5. Use external style sheets and scripts to keep the HTML concise.

How do I stay up-to-date with the latest HTML standards and best practices? How do I stay up-to-date with the latest HTML standards and best practices? Jun 20, 2025 am 08:33 AM

The key to keep up with HTML standards and best practices is to do it intentionally rather than follow it blindly. First, follow the summary or update logs of official sources such as WHATWG and W3C, understand new tags (such as) and attributes, and use them as references to solve difficult problems; second, subscribe to trusted web development newsletters and blogs, spend 10-15 minutes a week to browse updates, focus on actual use cases rather than just collecting articles; second, use developer tools and linters such as HTMLHint to optimize the code structure through instant feedback; finally, interact with the developer community, share experiences and learn other people's practical skills, so as to continuously improve HTML skills.

How do I use the  element to represent the main content of a document? How do I use the element to represent the main content of a document? Jun 19, 2025 pm 11:09 PM

The reason for using tags is to improve the semantic structure and accessibility of web pages, make it easier for screen readers and search engines to understand page content, and allow users to quickly jump to core content. Here are the key points: 1. Each page should contain only one element; 2. It should not include content that is repeated across pages (such as sidebars or footers); 3. It can be used in conjunction with ARIA properties to enhance accessibility. Usually located after and before, it is used to wrap unique page content, such as articles, forms or product details, and should be avoided in, or in; to improve accessibility, aria-labeledby or aria-label can be used to clearly identify parts.

How do I create a basic HTML document? How do I create a basic HTML document? Jun 19, 2025 pm 11:01 PM

To create a basic HTML document, you first need to understand its basic structure and write code in a standard format. 1. Use the declaration document type at the beginning; 2. Use the tag to wrap the entire content; 3. Include and two main parts in it, which are used to store metadata such as titles, style sheet links, etc., and include user-visible content such as titles, paragraphs, pictures and links; 4. Save the file in .html format and open the viewing effect in the browser; 5. Then you can gradually add more elements to enrich the page content. Follow these steps to quickly build a basic web page.

How do I create checkboxes in HTML using the  element? How do I create checkboxes in HTML using the element? Jun 19, 2025 pm 11:41 PM

To create an HTML checkbox, use the type attribute to set the element of the checkbox. 1. The basic structure includes id, name and label tags to ensure that clicking text can switch options; 2. Multiple related check boxes should use the same name but different values, and wrap them with fieldset to improve accessibility; 3. Hide native controls when customizing styles and use CSS to design alternative elements while maintaining the complete functions; 4. Ensure availability, pair labels, support keyboard navigation, and avoid relying on only visual prompts. The above steps can help developers correctly implement checkbox components that have both functional and aesthetics.

What is an HTML tag? What is an HTML tag? Jun 13, 2025 am 12:36 AM

HTMLtagsareessentialforstructuringwebpages.Theydefinecontentandlayoutusinganglebrackets,ofteninpairslikeand,withsomebeingself-closinglike.HTMLtagsarecrucialforcreatingstructured,accessible,andSEO-friendlywebpages.

See all articles