Code Monkey home page Code Monkey logo

Comments (2)

Kamoba avatar Kamoba commented on June 25, 2024

I made it!
Example:

<!DOCTYPE html>
<html>
  <head>
    <title>Jcrop Example</title>
    <link rel="stylesheet" href="style.css" />
    <link rel="stylesheet" href="jcrop.css" />
  </head>
  <body>

    <div style="padding:0 5%;">
    <h1 style="font-family:Helvetica,sans-serif;">
      Jcrop Example <span style="color:lightgray;">- draw some crops!</span>
    </h1>

    <img id="target" src="https://d3o1694hluedf9.cloudfront.net/market-750.jpg">

    </div>

    
	<canvas id="myCanvas" width="100" height="50" style="border:1px solid #d3d3d3;">
    Your browser does not support the HTML5 canvas tag.</canvas>
	
    <script src="jcrop.js"></script>

    <script>
	  setImage();
      var jcp;
      Jcrop.load('target').then(img => {
        jcp = Jcrop.attach(img,{multi:false});
        const rect = Jcrop.Rect.sizeOf(jcp.el);
        jcp.newWidget(rect.scale(.7,.5).center(rect.w,rect.h));
        jcp.focus();
		jcp.setOptions({shadeOpacity:0.8});
      });
	  
      // to fix security issue when trying to convert to Data URI
      function setImage(){
	    document.getElementById('target').setAttribute('crossOrigin', 'anonymous');
        document.getElementById('target').src = 'https://d3o1694hluedf9.cloudfront.net/market-750.jpg';
      }

	  function getCoord(){
		console.log(jcp.active.pos);
		//document.querySelector("div.jcrop-widget.active").style.background = "red";
		return jcp.active.pos;
	  }
	  setTimeout(function(){
	  document.querySelector("div.jcrop-widget.active").addEventListener("click", getCoord);
	  },1000);
	  

	var c = document.getElementById("myCanvas");
	var ctx = c.getContext("2d");

	function cropImage() {
		let coords = getCoord();
		console.log("coords",coords);
		// reset canvas
		c.setAttribute("width", coords.w) ;
		c.setAttribute("height", coords.h);
		ctx.clearRect(0, 0, coords.w, coords.h);  // clear canvas
		// add cropped image in canvas
		var img = document.getElementById("target"); 
		ctx.drawImage(img, coords.x,coords.y,coords.w,coords.h,0,0,coords.w,coords.h);   //drawImage(sx,sy,swidth,sheight,x,y,width,height)
		// convert canvas to DATA URI
		var imgData = c.toDataURL("image/png").replace("image/png", "image/octet-stream"); 
		console.log(imgData);
	}
    </script>


<button onclick="cropImage()">Crop it!</button>
  </body>
</html>

from jcrop.

Utsaww avatar Utsaww commented on June 25, 2024

@Kamoba how to download image with multi-crop I set jcp = Jcrop.attach(img,{multi:false}); while console console.log(imgData); on button click user can also download the image how can we do, Please help

from jcrop.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.