本示例使用HTML5 canvas,简单的编写了上传头像的裁剪效果,移动端支持拖拽后裁剪, 虽然样式不好看,但是功能还算全:
下图为裁剪后的效果:
html部分:
- nbsp;html>
- html lang="en">
- head>
- meta charset="UTF-8">
- title>上传头像title>
- meta name="renderer" content="webkit">
- meta name="viewport" content="width=device-width, initial-scale=1.0">
- head>
- body>
- div id="imgCrop" style="width:200px;height:200px;border:1px solid #ccc;overflow:hidden;">
- img src="img/test.jpg" alt="">
- div>
- input type="file" accept="image/*" />
- button id="save">保存button>
- p>下面为剪切的图片:p>
- div id="imgShow">div>
- body>
- html>
JavaScript部分:
- var $imgCrop = $("#imgCrop");
- var $img = $imgCrop.find("img");
- var img = $img[0];
- var width = parseInt($imgCrop.css("width"));
- var height = parseInt($imgCrop.css("height"));
- var startX,startY,scale = 1;
- var x = 0,y = 0;
- $("input").on("change",function(){
- var fr = new FileReader();
- var file = this.files[0]
- //console.log(file);
- if(!/image\/\w+/.test(file.type)){
- alert(file.name + "不是图片文件!");
- return;
- }
- console.log(file);
- $img.removeAttr("height width");
- fr.readAsDataURL(file);
- fr.onload = function(){
- img.src = fr.result;
- var widthInit = img.width;
- if(img.width>img.height){
- img.height = height;
- x = (width - img.width)/2;
- y = 0;
- }else{
- img.width = width;
- x = 0;
- y = (height - img.height)/2;
- }
- scale = widthInit/img.width;
- move($img, x, y);
- };
- });
- img.addEventListener("touchstart",function(e){
- startX = e.targetTouches[0].pageX;
- startY = e.targetTouches[0].pageY;
- return;
- });
- img.addEventListener("touchmove",function(e){
- e.preventDefault();
- e.stopPropagation();
- var changeX = e.changedTouches[0].pageX - startX + x;
- var changeY = e.changedTouches[0].pageY - startY + y;
- move($(this), changeX, changeY);
- return;
- });
- img.addEventListener("touchend",function(e){
- var changeX = e.changedTouches[0].pageX - startX + x;
- var changeY = e.changedTouches[0].pageY - startY + y;
- x = x + e.changedTouches[0].pageX - startX;
- y = y + e.changedTouches[0].pageY - startY;
- move($(this), changeX, changeY);
- return;
- });
- //确定目标图片的样式
- function move(ele, x, y){
- ele.css({
- '-webkit-transform' : 'translate3d(' + x + 'px, ' + y + 'px, 0)',
- 'transform' : 'translate3d(' + x + 'px, ' + y + 'px, 0)'
- });
- }
- $("#save").on("click",function(){
- var url = imageData($img);
- console.log(url);
-
$("#imgShow").html("
+url+" />");;
- });
- //裁剪图片
- function imageData($img) {
- var canvas = document.createElement('canvas');
- var ctx = canvas.getContext('2d');
- canvas.width = width ;
- canvas.height = height;
- ctx.drawImage(img, -x*scale, -y*scale, width*scale, height*scale, 0, 0, width, height);
- return canvas.toDataURL();
- }
以上就是本文的全部内容,希望对大家的学习有所帮助。

HTML5 is a key technology for building modern web pages, providing many new elements and features. 1. HTML5 introduces semantic elements such as, , etc., which enhances web page structure and SEO. 2. Support multimedia elements and embed media without plug-ins. 3. Forms enhance new input types and verification properties, simplifying the verification process. 4. Offer offline and local storage functions to improve web page performance and user experience.

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


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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version
Useful JavaScript development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment