gpt4 book ai didi

android - 在 Ionic App 中通过手指(触摸)裁剪图像

转载 作者:技术小花猫 更新时间:2023-10-29 11:05:36 26 4
gpt4 key购买 nike

我正在开发基于 ionic 的应用程序。我的 ionic 应用程序版本是 1.2.4。我想在我的应用程序中裁剪功能。我想通过 Touch 裁剪不规则形状的图像。所以任何人都有 Create touch cropper 所以请帮助我。

为了更清楚,看下面的gif我想要什么。

enter image description here

过去 2 天,我在谷歌上搜索解决方案,发现可以轻松进行简单裁剪或正方形或矩形裁剪,但无法通过触摸获取裁剪图像。

如果有人做过,请向我推荐正确的方向。

最佳答案

指针事件在移动设备中不起作用,因此这是经过一些修改后可在 Ionic 应用程序或任何跨平台应用程序中完全工作的代码。

 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.getElementById("exampleEle").appendChild(selectedContent);
var image = new Image; // the image
//image.src = "img/temp.png";
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(touch.but){
// clear the mask if on the right image
if(touch.oldBut === false && touch.x > 256){
selectLayer.ctx.clearRect(0,0,w,h);
touch.but = false;
}else{
// draw the red
selectLayer.ctx.fillStyle = "red";
fillCircle(touch.x, touch.y, 20, 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";

touch.oldBut = touch.but;
requestAnimationFrame(update);
}
requestAnimationFrame(update);
}
//#############################################################################
// helper functions not part of the answer
//#############################################################################
const touch = {
x : 0, y : 0, but : false,
events(e){
console.log("e.type",e);
const m = touch;
const bounds = canvas.getBoundingClientRect();
var rect = e.target.getBoundingClientRect();
if(e.targetTouches) {
X = parseInt(e.targetTouches[0].pageX - rect.left);
Y = parseInt(e.targetTouches[0].pageY - rect.top);
}
m.x = X;
m.y = Y;
m.but = e.type === "touchstart" ? true : e.type === "touchend" ? false : m.but;
}
};
(["start","end","move"]).forEach(name => document.addEventListener("touch" + name,touch.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()}

对于 View ,您必须添加这 3 行 html。

  <div id="exampleEle">

<canvas id="canvas" width=256 height=256></canvas>
</div>

关于android - 在 Ionic App 中通过手指(触摸)裁剪图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47323906/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com