


The following continues to introduce ## based on html5canvascanvas #Magnifying glass effect:
Main steps:1) Loading of images, there is www.php.cn/html5-tutorial-358646.html in the previous blog, you must pay attention to apache configuration, otherwise getImageData() will have security issues and cannot run!##2) Core: Mapping between two image matrices,
Set o as the center of the circle, then transform The last point A' corresponds to point A of the original image (this is the effect of magnification!!! In this experiment, the magnification factor is 2)
3) For the sake of simplicity, the linear interpolation method is not used, directly Round to get the coordinates of point A.
for (var j=0;j<image.height;j++){ for(var i=0;i<image.width;i++){ var k=4*(image.width*j+i); var k1=4*(image.width*Math.floor((j+y0)/2)+Math.floor((i+x0)/2)); ... else if(isIn(x0,y0,i,j,r)){//isIn()判定点是否在园内 if(k1>=0&&k1<=4*image.height*image.width){ imagedata2.data[k+0]=imagedata1.data[k1+0]; imagedata2.data[k+1]=imagedata1.data[k1+1]; imagedata2.data[k+2]=imagedata1.data[k1+2]; imagedata2.data[k+3]=255; // console.log('x:'+x+'y:'+y); }
4) In order to make the mirror stand out, set the point at the edge to a black point (that is, rgb is 0)
function isOn(x0,y0,x,y,r){//放大镜边缘 if((x0-x)*(x0-x)+(y0-y)*(y0-y)==r*r) return true; else return false; }
if (isOn(x0,y0,i,j,r)){ imagedata2.data[k+0]=0; imagedata2.data[k+1]=0; imagedata2.data[k+2]=0; imagedata2.data[k+3]=255; }
5) Out of bounds or wrap. point (if it exists), ignore it (just take the original value)
else{ imagedata2.data[k+0]=imagedata1.data[k+0]; imagedata2.data[k+1]=imagedata1.data[k+1]; imagedata2.data[k+2]=imagedata1.data[k+2]; imagedata2.data[k+3]=255; }
6) External setting of the magnifying glass radius
Also use canvas and onclick functions, as shown below:
<canvas id="bar" height="30" width="305"></canvas>  放大镜半径为<span id='r'></span> <script> var canvas2=document.getElementById('bar'); var context2=canvas2.getContext('2d'); context2.beginPath(); context2.lineWidth = 1;//边框宽度 context2.strokeStyle = "#ff00ff";//边框颜色 context2.strokeRect(10,0,295,28);//边框坐标及大小 context2.closePath(); canvas2.onclick=function(e){ var x1=e.clientX-e.target.offsetLeft; context2.clearRect(0,0,305,30);//擦除上次的痕迹 context2.beginPath(); context2.lineWidth = 1;//边框宽度 context2.strokeStyle = "#ff00ff";//边框颜色 context2.strokeRect(10,0,295,28);//边框坐标及大小 context2.fillStyle = "#00ff00";//填充canvas的背景颜色 context2.fillRect(0, 0, x1,28);//参数分别表示 x轴,y轴,宽度,高度 document.getElementById('r').innerHTML=Math.floor(x1/6); r=Math.floor(x1/6); } </script>
The following is the complete code:
<!DOCTYPE html> <html> <head> <title>canvas图像处理</title> </head> <body> <h1 id="canvas放大镜">canvas放大镜</h1> <canvas id="canvas1" width="600" height="450">是时候更换浏览器了<a href="http://firefox.com.cn/download/">点击下载firefox</a></canvas> <!--当浏览器不支持html5时,会显示连接提示下载firefox--> <script> var canvas1=document.getElementById('canvas1'); var context1=canvas1.getContext('2d'); image=new Image(); image.src="photo.jpg"; context1.drawImage(image,0,0);//绘制原始图像,(0,0)表示图像的左上角位与canvas画布的位置 var imagedata1=context1.getImageData(0,0,image.width,image.height); var imagedata2=context1.createImageData(image.width,image.height); r=40;//放大镜半径,原始默认值,可以通过最下面的进度条设置 canvas1.onmousemove=function(e){ var x=e.clientX-e.target.offsetLeft; var y=e.clientY-e.target.offsetTop; x0=x;y0=y; //console.log('x:'+x+'y:'+y); for (var j=0;j<image.height;j++){ for(var i=0;i<image.width;i++){ var k=4*(image.width*j+i); var k1=4*(image.width*Math.floor((j+y0)/2)+Math.floor((i+x0)/2));//对应的原始图像上的点 if (isOn(x0,y0,i,j,r)){//放大镜边缘上的点 imagedata2.data[k+0]=0; imagedata2.data[k+1]=0; imagedata2.data[k+2]=0; imagedata2.data[k+3]=255; } else if(isIn(x0,y0,i,j,r)){//放大区域的点 if(k1>=0&&k1<=4*image.height*image.width){//放大效果的的时候,这一步其实可以省略 imagedata2.data[k+0]=imagedata1.data[k1+0]; imagedata2.data[k+1]=imagedata1.data[k1+1]; imagedata2.data[k+2]=imagedata1.data[k1+2]; imagedata2.data[k+3]=255; // console.log('x:'+x+'y:'+y); } } else{//其他剩下不受影响的点 imagedata2.data[k+0]=imagedata1.data[k+0]; imagedata2.data[k+1]=imagedata1.data[k+1]; imagedata2.data[k+2]=imagedata1.data[k+2]; imagedata2.data[k+3]=255; } }} context1.putImageData(imagedata2,0,0);//canvas显示 } canvas1.onmouseout=function(){context1.drawImage(image,0,0);}//鼠标移出,自动重绘 function isIn(x0,y0,x,y,r){//放大区域 if((x0-x)*(x0-x)+(y0-y)*(y0-y)<r*r) return true; else return false; } function isOn(x0,y0,x,y,r){//放大镜边缘 if((x0-x)*(x0-x)+(y0-y)*(y0-y)==r*r) return true; else return false; } </script> <br/> <canvas id="bar" height="30" width="305"></canvas>  放大镜半径为<span id='r'></span> <script> var canvas2=document.getElementById('bar'); var context2=canvas2.getContext('2d'); context2.beginPath(); context2.lineWidth = 1;//边框宽度 context2.strokeStyle = "#ff00ff";//边框颜色 context2.strokeRect(10,0,295,28);//边框坐标及大小 context2.closePath(); canvas2.onclick=function(e){ var x1=e.clientX-e.target.offsetLeft; context2.clearRect(0,0,305,30); context2.beginPath(); context2.lineWidth = 1;//边框宽度 context2.strokeStyle = "#ff00ff";//边框颜色 context2.strokeRect(10,0,295,28);//边框坐标及大小 context2.fillStyle = "#00ff00";//填充canvas的背景颜色 context2.fillRect(0, 0, x1,28);//参数分别表示 x轴,y轴,宽度,高度 document.getElementById('r').innerHTML=Math.floor(x1/6); r=Math.floor(x1/6);//一个划算,使得最大半径为50px } </script> </body> </html>
Attention:
1) Remember to set the width and height of canvas1 to be the same size as the
picture. If they are different, set the It is troublesome to perform this calculation;The effect is as follows:
The above is the detailed content of Detailed explanation of HTML5 implementation of partial image magnifying glass (adjustable) (graphics and text). For more information, please follow other related articles on the PHP Chinese website!

Best practices for H5 code include: 1. Use correct DOCTYPE declarations and character encoding; 2. Use semantic tags; 3. Reduce HTTP requests; 4. Use asynchronous loading; 5. Optimize images. These practices can improve the efficiency, maintainability and user experience of web pages.

Web standards and technologies have evolved from HTML4, CSS2 and simple JavaScript to date and have undergone significant developments. 1) HTML5 introduces APIs such as Canvas and WebStorage, which enhances the complexity and interactivity of web applications. 2) CSS3 adds animation and transition functions to make the page more effective. 3) JavaScript improves development efficiency and code readability through modern syntax of Node.js and ES6, such as arrow functions and classes. These changes have promoted the development of performance optimization and best practices of web applications.

H5 is not just the abbreviation of HTML5, it represents a wider modern web development technology ecosystem: 1. H5 includes HTML5, CSS3, JavaScript and related APIs and technologies; 2. It provides a richer, interactive and smooth user experience, and can run seamlessly on multiple devices; 3. Using the H5 technology stack, you can create responsive web pages and complex interactive functions.

H5 and HTML5 refer to the same thing, namely HTML5. HTML5 is the fifth version of HTML, bringing new features such as semantic tags, multimedia support, canvas and graphics, offline storage and local storage, improving the expressiveness and interactivity of web pages.

H5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo

The tools and frameworks that need to be mastered in H5 development include Vue.js, React and Webpack. 1.Vue.js is suitable for building user interfaces and supports component development. 2.React optimizes page rendering through virtual DOM, suitable for complex applications. 3.Webpack is used for module packaging and optimize resource loading.

HTML5hassignificantlytransformedwebdevelopmentbyintroducingsemanticelements,enhancingmultimediasupport,andimprovingperformance.1)ItmadewebsitesmoreaccessibleandSEO-friendlywithsemanticelementslike,,and.2)HTML5introducednativeandtags,eliminatingthenee

H5 improves web page accessibility and SEO effects through semantic elements and ARIA attributes. 1. Use, etc. to organize the content structure and improve SEO. 2. ARIA attributes such as aria-label enhance accessibility, and assistive technology users can use web pages smoothly.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor