Crop image via finger(touch) in Ionic App

前端 未结 3 854
夕颜
夕颜 2021-02-20 03:40

I am Working on ionic based Application.My ionic app version is 1.2.4. I want to Crop Functionality in my application. I want to crop image Via Touch with irregular Shape. So an

3条回答
  •  梦谈多话
    2021-02-20 04:04

    Basically I'm not ionic developer but After googling I found some code that might be helpful for you.

    setTimeout(example,0); // ensures that the run us after parsing
    function example(){
      const ctx = canvas.getContext("2d");
      var w = canvas.width;
      var h = canvas.height;
      var cw = w / 2;  // center 
      var ch = h / 2;
    
      var selectLayer = CImageCtx(w,h); // creates a canvas 
      var selectedContent = CImageCtx(w,h); // the selected content
      document.body.appendChild(selectedContent);
      var image = new Image;  // the image
      image.src ="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e9/Official_portrait_of_Barack_Obama.jpg/220px-Official_portrait_of_Barack_Obama.jpg";
      // updates the masked result
      function updateSelected(){
        var ctx = selectedContent.ctx;
        ctx.drawImage(image,0,0);
        ctx.globalCompositeOperation = "destination-in";
        ctx.drawImage(selectLayer,0,0);
        ctx.globalCompositeOperation = "source-over";
      }
      function update(){
          // if mouse down then 
          if(mouse.but){
            // clear the mask if on the right image
            if(mouse.oldBut === false && mouse.x > 256){
               selectLayer.ctx.clearRect(0,0,w,h);
               mouse.but = false;
            }else{
               // draw the red 
               selectLayer.ctx.fillStyle = "red";
               fillCircle(mouse.x, mouse.y, 10, selectLayer.ctx);
            }
            // update the masked result
            updateSelected();
          }
    
          // clear the canvas
          ctx.clearRect(0,0,w,h);
          // draw the image
          ctx.drawImage(image,0,0);
          // then draw the marking layer over it with comp overlay
          ctx.globalCompositeOperation = "overlay";
          ctx.drawImage(selectLayer,0,0);
          ctx.globalCompositeOperation = "source-over";
    
          mouse.oldBut = mouse.but;
          requestAnimationFrame(update);
      }
      requestAnimationFrame(update);
    }
    
    const mouse  = {
      x : 0, y : 0, but : false,
      events(e){
        const m = mouse;
        const bounds = canvas.getBoundingClientRect();
        m.x = e.pageX - bounds.left - scrollX;
        m.y = e.pageY - bounds.top - scrollY;
        m.but = e.type === "pointerdown" ? true : e.type === "pointerup" ? false : m.but;
      }
    };
    
    document.addEventListener('pointerdown', mouse.events);
    document.addEventListener('pointerup', mouse.events);
    document.addEventListener('pointermove', mouse.events);
    
    const CImage = (w = 128, h = w) => (c = document.createElement("canvas"),c.width = w,c.height = h, c);
    const CImageCtx = (w = 128, h = w) => (c = CImage(w,h), c.ctx = c.getContext("2d"), c);
    const fillCircle = (l,y=ctx,r=ctx,c=ctx) =>{if(l.p1){c=y; r=leng(l);y=l.p1.y;l=l.p1.x }else if(l.x){c=r;r=y;y=l.y;l=l.x}c.beginPath(); c.arc(l,y,r,0,Math.PI*2); c.fill()}
    Draw on image and the selected parts are shown on the right
    Click right image to reset selection

    Check to run code snippet.

提交回复
热议问题