Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the effect of Nine Palace Grid cut by canvas in JavaScript

2025-04-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to achieve the nine palace grid cut effect of canvas in JavaScript. I hope you will get something after reading this article. Let's discuss it together.

First of all, the page is displayed

Go directly to the code

Document body {text-align: center;} canvas {border: 1px solid;} .newcanvas {width: 316px; height: 316px; margin: auto;} .newpohoto, .download {width: 300px Height: 40px; line-height: 40px; margin: auto; background-color: cornflowerblue; border-radius: 5px; cursor: pointer; margin: 10px auto; color: white } use canvas to achieve the effect of cutting images in the ninth palace. Start cutting and downloading pictures var canvas = document.getElementById ("mycnavas") / / now put the picture on var cxt = mycnavas.getContext ("2d"); var img = new Image (); img.src = ".. / img/img10.jpg"; _ window.onload = function () {cxt.drawImage (img, 0,0400,300); / / the position where the picture is drawn} var arr = [] / save the cut images in the array document.getElementsByClassName ("newpohoto") [0] .onclick = function () {var Q = 1; for (var I = 0; I < 3; iSense +) {for (var j = 0; j < 3; jacks +) {var data = cxt.getImageData (j * 100,100,400,100) / / similar to the "copy" function var img = document.getElementById ("img" + Q) var newcxt = img.getContext ("2d"); newcxt.putImageData (data, 0,0) / / two parameters of the similar "paste" function arr.push (console.log (img.toDataURL (Q + ".png")) / / toDataURL () method: DataURL (type, encoderOptions) type specifies the format of the image converted to base64 encoding, such as image/png, image/jpeg, image/webp, etc., which defaults to image/png format Qupload images; console.log (arr)} / / download cut images var arr = []; document.getElementsByClassName ('download') [0] .onclick = function () {for (var I = 0; I < 9) Var +) {var a = document.createElement ('a'); a.download = 'img' + (I + 1); a.href = arr [I]; document.body.appendChild (a); a.click () }} after reading this article, I believe you have a certain understanding of "how to achieve the nine-grid cut effect of canvas in JavaScript". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report