JS image area can be clicked, adapted to the mobile side, the size of the image changes randomly

Source: Internet
Author: User

The implementation of the picture area clickable, in fact, the use of map is possible, but the adaptation effect is not good, the image can only be fixed-size values, and points are written dead.

Here, I'm using JS based on a canvas to write a gadget. You can circle the part you need to click, then generate a bunch of JSON, and you'll see the effect on the preview page;

In the actual application, as long as the tool to deal with a piece, and then put the data into the database, it is very convenient;

When using the tool, upload the picture first. Then you can circle, the circle must remember, click to save the data,, and then you can preview, preview the effect of the click, you can look at the console,

First, the tools.

First the HTML of the tool

<! DOCTYPE html>

 

Then the JS Code of the tool

var canvas = document.getElementById (' canvas '); var ctx = Canvas.getcontext (' 2d '); ctx.linewidth = 1; Ctx.strokestyle= "Green";/*ctx.beginpath (); Ctx.moveto (10, 100); Ctx.lineto (300,100); Ctx.stroke (); Ctx.closepath (); */var run = False;var modulelist = [];var path = [];var $baseImg = '; var $cover = $ (". Cover"); var $urlAdd ress = $ (' #urlAddress '), var $dec = $ ("#dec"), var $baseWidth = $ (' #width '), var $baseHeight = $ (' #height '); Canvas.onmousedo WN = function (e) {//console.log (E.CLIENTX);//console.log (E.OFFSETX); Ctx.beginpath (); Ctx.moveto (E.offsetx, e.offsety);p Ath.push ({x:e.offsetx,y:e.offsety}); run = true;} Canvas.onmousemove = function (e) {//var x = e.offsetx;if (run) {Ctx.lineto (e.offsetx, e.offsety); Ctx.stroke ();p Ath.push ({x:e.offsetx,y:e.offsety});}} Canvas.onmouseup = function (e) {run = False;ctx.closepath (), if (Path.length >) {$cover. Removeclass (' hide ');} Else{path = [];}} Save data Function SaveData () {var data = {"Dec": "Picture Click", "Version": "1", "img": $BASEIMG, "module": Modulelist, "Basewidth": $ BaseWidth.val (), "BaseHeight": $baseHeight. Val ()}console.log (json.stringify (data)); Localstorage.setitem ("Data"), Json.stringify (data));} View data function Seedata (flag) {var data = {"Dec": "Picture Click", "Version": "1", "img": $BASEIMG, "module": Modulelist, "Basewidth" : $baseWidth. Val (), "BaseHeight": $baseHeight. Val ()}if (flag) {Console.log (json.stringify (data));} return data;} Picture background $ (' #imgload '). On (' Change ', function () {imgToBase64 (This.files[0],function (Result) {Console.log (result); var base64data = ' url (' + result + ') '; $ (canvas). css ({' Background-image ': Base64data}); $baseImg = result;});    /converted to Base64function imgToBase64 (file,callback) {var reader = new FileReader ();    Reader.onload = function (e) {callback (E.target.result);    }; Reader.readasdataurl (file); After reading, the OnLoad method is called}//acknowledgment information $cover.on (' click ', '. contain ', function () {if ($urlAddress. val () = = ") {alert (' address cannot be null ');} Else{modulelist.push ({id:getname (), Path:path,url: $urlAddress. Val (), Dec: $dec. val ()});p ath = []; $cover. addclass (' Hide ');}); /modify height and width $basewidth. On ("Change", function () {$ (canvas). CSS ({' Width ': $ (this). val ()});}); $baseHeight. On ("Change", function () {$ (canvas). css ({' Height ': $ (this). val ()});}); /random Get Name Function getName () {var timer = new Date (); var arr = json.stringify (timer). Replace (/:|-| " /g, '). Split ('. '); var str = arr.join ("); return str;} Import Template function GetData () {var data = Json.parse (Localstorage.getitem ("Data")), if (data) {modulelist = Data.module;; $BASEIMG = data.img;drawn (data);} Else{alert (' No template data. ');}} Revoke function Reduo () {modulelist.pop (); ctx.clearrect (0, 0, canvas.width, canvas.height);d Rawn (Seedata ());} Clears all function ClearAll () {modulelist = [];ctx.clearrect (0, 0, canvas.width, canvas.height);} Give the data, draw out function drawn (data) {ctx.clearrect (0, 0, canvas.width, canvas.height); var module = Data.module;var Base64data = ' url (' + data.img + ') '; $ (canvas). css ({' Background-image ': Base64data}); $baseWidth. Val (data.basewidth); $ Baseheight.val (Data.baseheight); $ (canvas). CSS ({' width ': data.basewidth, ' height ':d ata.baseheight});//start drawing for (Var i= 0; I < module.length;i++) {var path = Module[i].path;ctx.beginpath (); Ctx.moveto (path[0].x, PATH[0].Y); for (var j = 1; J &lt ; Path.length; J + +) {Ctx.lineto (path[j].x, PATH[J].Y); Ctx.stroke ();} Ctx.closepath ();}}

  

 

And finally the preview.html preview

<! DOCTYPE html>

  

Note the introduction of the JS and jauery paths:

Experience Address: If necessary, contact the blogger

JS image area can be clicked, adapted to the mobile side, the size of the image changes randomly

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.