Home > Web Front-end > H5 Tutorial > body text

Canvas implements picture graffiti function (with code)

不言
Release: 2018-11-16 17:17:58
forward
5576 people have browsed it

The content of this article is about realizing the image graffiti function on canvas (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Requirements

  1. You need to annotate pictures and export them.

  2. N multiple pictures need to be marked and finally saved at the same time.

  3. Needs to be labeled based on polygon area data (area, color, name).

Corresponding solution

  1. Use canvas to draw graffiti, circles, and rectangles, and finally generate image base64 encoding for uploading

  2. Batch uploading a large number of pictures is time-consuming. In order to improve the user experience, we only implement circle and rectangle drawing, and finally save them as coordinates, and then draw them according to the coordinates the next time they are displayed.

  3. The display of the polygon area is drawn based on the coordinate points, and the position where the name is displayed is the polygon centroid.

Code



Copy after login

Must be passed in parameters

  • Picture path

url: string
Copy after login
  • Drawing area width

width: string
Copy after login
  • Drawing area height

height: string
Copy after login

Select whether the parameters passed in

  • ## can be drawn, the default is true

canDraw: boolean
Copy after login
Copy after login
  • Coordinate point information, if not passed in, it will not be drawn.

info: string
Copy after login
  • Whether it can be drawn, the default is true

canDraw: boolean
Copy after login
Copy after login
  • Drawing color, default red

lineColor: string
Copy after login
  • Drawing pen width, default 2

lineWidth: number
Copy after login
  • Drawing pen type, rec, circle, default rec

lineType: string
Copy after login

Methods that can be called

  • Clear the canvas

clean()
Copy after login
  • Return coordinate point information

getInfo()
Copy after login

Special instructions

  • The canvas object cannot obtain coordinates. It is obtained through the coordinates of the parent element. Therefore, there cannot be too much positioning and nesting at the level above the parent element of the component, otherwise the drawing coordinates will be offset.

  • Images with different domain names may have cross-domain problems. I have read a lot of information but there is no good solution. In the final project, I used the node service to create an interface for converting images to base64, and then Solved by drawing on canvas. It may not be applicable to other projects. If there is a better solution, please share it.

  • Exporting coordinate point data can only export the coordinate points of regular patterns, because if there are too many coordinate points scribbled randomly, it will collapse (although I have not tried to what extent it will collapse). If If you have high-performance implementation methods, please share them.

  • If after saving the graffiti and then requesting the image URL, the request cannot be made, it is because of the CDN cache problem. This can be solved by adding a random code after the image path.

The above is the detailed content of Canvas implements picture graffiti function (with code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!