简体   繁体   English

使用AngularJS进行图像裁剪

[英]Image crop with AngularJS

I want to let the user crop an image, I found this JQuery plugin - http://deepliquid.com/content/Jcrop.html 我想让用户裁剪图像,我找到了这个JQuery插件 - http://deepliquid.com/content/Jcrop.html

I tried to use it with Angular-ui's Jquery passthrough option, adding the ui-jq=Jcrop directive to the <img> 我尝试将它与Angular-ui的Jquery passthrough选项一起使用,将ui-jq=Jcrop指令添加到<img>

The problem is that If I use ng-src to dynamically change the image it doesn't work and nothing is seen. 问题是如果我使用ng-src动态更改图像它不起作用,看不到任何东西。 If I change it to src and put a static url I can see the image and Jcrop. 如果我将其更改为src并放置一个静态URL我可以看到图像和Jcrop。

how can I fix that ? 我该如何解决这个问题? also, how can I listen to Jcrop's callbacks to know what is the user's selection ? 另外,我怎样才能听Jcrop的回调知道用户的选择是什么?

is there a better / simpler way to add image cropping functionality to AngularJS ? 是否有更好/更简单的方法将图像裁剪功能添加到AngularJS?

Here is my solution: 这是我的解决方案:

I've written a directive that create img element and apply plugin on it. 我写了一个指令,创建img元素并在其上应用插件。 When src is changed, this img is removed and content that was created by plugin is also destroyed and then re-created new img with new src and again applied plugin on it. 当src被更改时,这个img被删除,插件创建的内容也被破坏,然后用新的src重新创建新的img并再次应用插件。

Also provided 'selected' callback to be able to get coordinated that were selected (wrapped in $apply so you can modify your scope values in it). 还提供了“选定”回调,以便能够进行选择的协调(包装在$ apply中,以便您可以修改其中的范围值)。

Check my solution at Plunker 在Plunker检查我的解决方案

I've built a demo using AngularJS and Jcrop here: 我在这里使用AngularJS和Jcrop构建了一个演示:

Demo: https://coolaj86.github.com/angular-image-crop 演示: https//coolaj86.github.com/angular-image-crop

On Github: https://github.com/coolaj86/angular-image-crop 在Github上: https//github.com/coolaj86/angular-image-crop

You can leverage ui-event to create an event definition object with the keys being the event names and the values being the callbacks. 您可以利用ui-event创建一个事件定义对象,其中键是事件名称,值是回调。 Or you can simply pass these events as options to Jcrop ( according to the documentation ) 或者你可以简单地将这些事件作为选项传递给Jcrop( 根据文档

Finally, there is a new update coming to ui-jq that lets you add ui-refresh which is an expression to be watched to re-trigger the plugin. 最后, ui-jq还有一个新的更新 ,允许你添加ui-refresh ,这是一个需要重新触发插件的表达式。

Theoretically you should be able to do 从理论上讲,你应该可以做到

<img ui-jq="Jcrop" 
     ui-options="{onSelect:myCallback}"
     ui-event="{onChange:'myCallback($event)'}"
     ui-refresh="imgSrc"
     ng-src="imgSrc" />

Note: this simply re-fires the passthrough again, and doesn't automatically mean this will fix the problem or that the plugin will behave properly when re-initialized 注意:这只是重新启动passthrough,并不会自动意味着这将解决问题或者插件在重新初始化时将正常运行

We're still working on a good way to allow you to trigger different events at different times. 我们仍在努力让您在不同时间触发不同的事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM