Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize fillet Picture with canvas in js

Shulou Source: shulou.com Published: 2022-06-03 20:05:05 11月12日 Update

This article mainly shows you "how to achieve fillet pictures in js", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to achieve rounded corners in js canvas" this article.

The code implementation of the fillet picture:

Title canvas to draw

Window. "nl" ad=function () {var drawing = document.getElementById ('drawing'); if (drawing.getContext) {print (' support') addRoundRectFunc (); var context = drawing.getContext ('2d'); draw (context) } else {print ('not')}} function draw (context) {context.fillStyle = 'red'; var image = document.images [0]; context.roundRect Context.drawImage (image, 0,0, image.width / 2, image.height / 2) / / toImage ();} function addRoundRectFunc () {CanvasRenderingContext2D.prototype.roundRect = function (x, y, width, height, radius, fill, stroke) {if (typeof stroke = = "undefined") {stroke = true } if (typeof radius = "undefined") {radius = 5;} this.beginPath (); this.moveTo (x + radius, y); this.lineTo (x + width-radius, y); this.quadraticCurveTo (x + width, y, x + width, y + radius) This.lineTo (x + width, y + height-radius); this.quadraticCurveTo (x + width, y + height, x + width-radius, y + height); this.lineTo (x + radius, y + height); this.quadraticCurveTo (x, y + height, x, y + height-radius); this.lineTo (x, y + radius) This.quadraticCurveTo (x, y, x + radius, y); this.closePath (); if (stroke) {this.stroke ();} if (fill) {this.fill ();}} } function toImage () {var imageUri = drawing.toDataURL ('image/png'); var imageTag = document.createElement (' img'); imageTag.style = 'margin:10px;width:200px;height:200px' imageTag.src = imageUri; document.body.appendChild (imageTag)} function print (txt) {document.getElementById ("container") [xss_clean] + = ('\ n') + txt } document.body.onclick = function () {_ window.location.reload ()} console.log = print

Effect picture:

Add a small code: canvas to generate fillet pictures (avatars, etc.)

CircleImg (ctx, img, x, y, r) {ctx.save (); var d = 2 * r; var cx = x + r; var cy = y + r; ctx.arc (cx, cy, r, 0,2 * Math.PI); ctx.clip (); ctx.drawImage (img, x, y, d, d); ctx.restore () } these are all the contents of the article "how to achieve rounded corners in canvas in js". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Pictures rounded corners content articles code learning help avatars effects effect drawings easy to understand more organization knowledge editing industry information information channels channels generation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Xiaomi Apple MySQL NVidia