


What is canvas off-screen technology? How to achieve canvas magnifying glass effect?
The content of this article is about what is canvas off-screen technology? How to achieve canvas magnifying glass effect? , has certain reference value, friends in need can refer to it, I hope it will be helpful to you.
Usingcanvas
In addition to implementing filters, you can also use off-screen technologymagnifying glass function. For the convenience of explanation, this article is divided into 2 application parts:
Implementing watermark and center scaling
Implementing magnifying glass
1. What is off-screen technology?
Canvas learning and filter implementation have introduced the drawImage
interface. In addition to drawing images, this interface can also: Draw a canvas
object to another canvas
object. This is off-screen technology.
2. Implement watermark and center scaling
In the code, there are two canvas tags. They are visible and invisible respectively. The Context object on the invisible canvas object is where we place the image watermark.
For more details, please see the code comments:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Learn Canvas</title> <style> canvas { display: block; margin: 0 auto; border: 1px solid #222; } input { display: block; margin: 20px auto; width: 800px } </style> </head> <body> <div id="app"> <canvas id="my-canvas"></canvas> <input type="range" value="1.0" min="0.5" max="3.0" step="0.1"> <canvas id="watermark-canvas" style="display: none;"></canvas> </div> <script type="text/javascript"> window.onload = function () { var canvas = document.querySelector("#my-canvas") var watermarkCanvas = document.querySelector("#watermark-canvas") var slider = document.querySelector("input") var scale = slider.value var ctx = canvas.getContext('2d') var watermarkCtx = watermarkCanvas.getContext("2d") /* 给第二个canvas获取的Context对象添加水印 */ watermarkCanvas.width = 300 watermarkCanvas.height = 100 watermarkCtx.font = "bold 20px Arial" watermarkCtx.lineWidth = "1" watermarkCtx.fillStyle = "rgba(255 , 255 , 255, 0.5)" watermarkCtx.fillText("=== yuanxin.me ===", 50, 50) /****************************************/ var img = new Image() img.src = "./img/photo.jpg" /* 加载图片后执行操作 */ img.onload = function () { canvas.width = img.width; canvas.height = img.height; drawImageByScale(canvas, ctx, img, scale, watermarkCanvas); // 监听input标签的mousemove事件 // 注意:mousemove实时监听值的变化,内存消耗较大 slider.onmousemove = function () { scale = slider.value drawImageByScale(canvas, ctx, img, scale, watermarkCanvas); } } /******************/ } /** * * @param {Object} canvas 画布对象 * @param {Object} ctx * @param {Object} img * @param {Number} scale 缩放比例 * @param {Object} watermark 水印对象 */ function drawImageByScale(canvas, ctx, img, scale, watermark) { // 图像按照比例进行缩放 var width = img.width * scale, height = img.height * scale // (dx, dy): 画布上绘制img的起始坐标 var dx = canvas.width / 2 - width / 2, dy = canvas.height / 2 - height / 2 ctx.clearRect(0, 0, canvas.width, canvas.height) // No1 清空画布 ctx.drawImage(img, dx, dy, width, height) // No2 重新绘制图像 if (watermark) { // No3 判断是否有水印: 有, 绘制水印 ctx.drawImage(watermark, canvas.width - watermark.width, canvas.height - watermark.height) } } </script> </body> </html>
The implementation effect is shown below:
Drag the slider to zoom in and out of the image. Then right click to save the image. The saved image will already have a watermark, as shown below:
3. Implement the magnifying glass
in the above On the basis of center zoom, the owner of the magnifying glass needs to pay attention to the following two parts:
Refined processing of
canvas
's mouse response events: slide in, slide out, click and releaseRecalculate the off-screen coordinates (see the code comments for detailed formula calculation ideas)
Recalculate the mouse relative to the canvas label Coordinates (see code comments for detailed formula calculation ideas)
The code is as follows:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> <style> canvas { display: block; margin: 0 auto; border: 1px solid #222; } </style> </head> <body> <canvas id="my-canvas"></canvas> <canvas id="off-canvas" style="display: none;"></canvas> <script> var isMouseDown = false, scale = 1.0 var canvas = document.querySelector("#my-canvas") var offCanvas = document.querySelector("#off-canvas") // 离屏 canvas var ctx = canvas.getContext("2d") var offCtx = offCanvas.getContext("2d") // 离屏 canvas 的 Context对象 var img = new Image() window.onload = function () { img.src = "./img/photo.jpg" img.onload = function () { canvas.width = img.width canvas.height = img.height offCanvas.width = img.width offCanvas.height = img.height // 计算缩放比例 scale = offCanvas.width / canvas.width // 初识状态下, 两个canvas均绘制Image ctx.drawImage(img, 0, 0, canvas.width, canvas.height) offCtx.drawImage(img, 0, 0, canvas.width, canvas.height) } // 鼠标按下 canvas.onmousedown = function (event) { event.preventDefault() // 禁用默认事件 var point = windowToCanvas(event.clientX, event.clientY) // 获取鼠标相对于 canvas 标签的坐标 isMouseDown = true drawCanvasWithMagnifier(true, point) // 绘制在离屏canvas上绘制放大后的图像 } // 鼠标移动 canvas.onmousemove = function (event) { event.preventDefault() // 禁用默认事件 if (isMouseDown === true) { var point = windowToCanvas(event.clientX, event.clientY) drawCanvasWithMagnifier(true, point) } } // 鼠标松开 canvas.onmouseup = function (event) { event.preventDefault() // 禁用默认事件 isMouseDown = false drawCanvasWithMagnifier(false) // 不绘制离屏放大镜 } // 鼠标移出canvas标签 canvas.onmouseout = function (event) { event.preventDefault() // 禁用默认事件 isMouseDown = false drawCanvasWithMagnifier(false) // 不绘制离屏放大镜 } } /** * 返回鼠标相对于canvas左上角的坐标 * @param {Number} x 鼠标的屏幕坐标x * @param {Number} y 鼠标的屏幕坐标y */ function windowToCanvas(x, y) { var bbox = canvas.getBoundingClientRect() // bbox中存储的是canvas相对于屏幕的坐标 return { x: x - bbox.x, y: y - bbox.y } } function drawCanvasWithMagnifier(isShow, point) { ctx.clearRect(0, 0, canvas.width, canvas.height) // 清空画布 ctx.drawImage(img, 0, 0, canvas.width, canvas.height) // 在画布上绘制图像 /* 利用离屏,绘制放大镜 */ if (isShow) { var { x, y } = point var mr = 50 // 正方形放大镜边长 // (sx, sy): 待放大图像的开始坐标 var sx = x - mr / 2, sy = y - mr / 2 // (dx, dy): 已放大图像的开始坐标 var dx = x - mr, dy = y - mr // 将offCanvas上的(sx,sy)开始的长宽均为mr的正方形区域 // 放大到 // canvas上的(dx,dy)开始的长宽均为 2 * mr 的正方形可视区域 // 由此实现放大效果 ctx.drawImage(offCanvas, sx, sy, mr, mr, dx, dy, 2 * mr, 2 * mr) } /*********************/ } </script> </body> </html>
The magnifying glass effect is as shown below (the area marked by the red pen is our Square magnifying glass):
Related recommendations:
[HTML5]Canvas achieves magnifying glass effect
html canvas implements screen capture
The above is the detailed content of What is canvas off-screen technology? How to achieve canvas magnifying glass effect?. For more information, please follow other related articles on the PHP Chinese website!

H5 (HTML5) will improve web content and design through new elements and APIs. 1) H5 enhances semantic tagging and multimedia support. 2) It introduces Canvas and SVG, enriching web design. 3) H5 works by extending HTML functionality through new tags and APIs. 4) Basic usage includes creating graphics using it, and advanced usage involves WebStorageAPI. 5) Developers need to pay attention to browser compatibility and performance optimization.

H5 brings a number of new functions and capabilities, greatly improving the interactivity and development efficiency of web pages. 1. Semantic tags such as enhance SEO. 2. Multimedia support simplifies audio and video playback through and tags. 3. Canvas drawing provides dynamic graphics drawing tools. 4. Local storage simplifies data storage through localStorage and sessionStorage. 5. The geolocation API facilitates the development of location-based services.

HTML5 brings five key improvements: 1. Semantic tags improve code clarity and SEO effects; 2. Multimedia support simplifies video and audio embedding; 3. Form enhancement simplifies verification; 4. Offline and local storage improves user experience; 5. Canvas and graphics functions enhance the visualization of web pages.

The core features of HTML5 include semantic tags, multimedia support, offline storage and local storage, and form enhancement. 1. Semantic tags such as, etc. to improve code readability and SEO effect. 2. Simplify multimedia embedding with labels. 3. Offline storage and local storage such as ApplicationCache and LocalStorage support network-free operation and data storage. 4. Form enhancement introduces new input types and verification properties to simplify processing and verification.

H5 provides a variety of new features and functions, greatly enhancing the capabilities of front-end development. 1. Multimedia support: embed media through and elements, no plug-ins are required. 2. Canvas: Use elements to dynamically render 2D graphics and animations. 3. Local storage: implement persistent data storage through localStorage and sessionStorage to improve user experience.

H5 and HTML5 are different concepts: HTML5 is a version of HTML, containing new elements and APIs; H5 is a mobile application development framework based on HTML5. HTML5 parses and renders code through the browser, while H5 applications need to run containers and interact with native code through JavaScript.

Key elements of HTML5 include,,,,,, etc., which are used to build modern web pages. 1. Define the head content, 2. Used to navigate the link, 3. Represent the content of independent articles, 4. Organize the page content, 5. Display the sidebar content, 6. Define the footer, these elements enhance the structure and functionality of the web page.

There is no difference between HTML5 and H5, which is the abbreviation of HTML5. 1.HTML5 is the fifth version of HTML, which enhances the multimedia and interactive functions of web pages. 2.H5 is often used to refer to HTML5-based mobile web pages or applications, and is suitable for various mobile devices.


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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download
The most popular open source editor

SublimeText3 Chinese version
Chinese version, very easy to use

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
