-
Notifications
You must be signed in to change notification settings - Fork 451
加入“非实时渲染”以解决频繁绘制 canvas 带来的卡顿问题 #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
5e5c42a
8843e16
6eeb488
c5cca95
3970b00
b8da0a8
aef5992
2c814fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -131,6 +131,18 @@ var DEFAULT = { | |
| tmp.cut = value; | ||
| } | ||
| }, | ||
| inRealTime: { | ||
| default: true, | ||
| get: function get () { | ||
| return tmp.inRealTime | ||
| }, | ||
| set: function set (value) { | ||
| if (typeof (value) !== 'boolean') { | ||
| console.error(('inRealTime:' + value + ' is invalid')); | ||
| } | ||
| tmp.inRealTime = value; | ||
| } | ||
| }, | ||
| onReady: { | ||
| default: null, | ||
| get: function get () { | ||
|
|
@@ -180,6 +192,7 @@ function prepare () { | |
| var pageContext = pages[pages.length - 1]; | ||
| // 把this依附在Page上下文的wecropper属性上,便于在page钩子函数中访问 | ||
| pageContext.wecropper = self; | ||
| self.pageContext = pageContext; | ||
| }; | ||
|
|
||
| self.createCtx = function () { | ||
|
|
@@ -622,6 +635,30 @@ function methods () { | |
| return self | ||
| }; | ||
|
|
||
| self.updateImage = function (isInit) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 能否有办法避免在库中setData呢,因为这样处理的话,和外部cropperOpt耦合了,能否做成外部的拓展模块,因为imgLeft、imgTop等属性是可以通过实例拿到的
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 如果要在外部 page 中更新 UI 的话,这就可以解决 这样会有个问题: 更好的方式应该是:开发者在使用 we-cropper 时,只需指定参数和监听事件,其他裁切相关的具体逻辑都无需过问。 如果将 如果以小程序的
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里我的考虑是,we-cropper理念还是设计成一个库,最终可以运行在各个平台(框架)上,尽可能脱离宿主上下文环境,目前we-cropper缺乏一套可靠的插件拓展机制,这个是一个必须解决的首要问题。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 更新你的代码以后,跑不起来呀,cropperImageSrc imageTop 这些属性都是获不到值,例子,应该是1.2版本的吧, |
||
| if ( isInit === void 0 ) isInit = false; | ||
|
|
||
| if (self.croperTarget && isInit) { | ||
| self.pageContext.setData({ | ||
| 'cropperOpt.cropperImageSrc': self.croperTarget | ||
| }); | ||
| } | ||
|
|
||
| if (!self.imgLeft || !self.imgTop) { | ||
| return | ||
| } | ||
|
|
||
| self.pageContext.setData({ | ||
| 'cropperOpt.imageLeft': self.imgLeft, | ||
| 'cropperOpt.imageTop': self.imgTop, | ||
| 'cropperOpt.imageWidth': self.scaleWidth, | ||
| 'cropperOpt.imageHeight': self.scaleHeight | ||
|
|
||
| }); | ||
|
|
||
| return self | ||
| }; | ||
|
|
||
| self.pushOrign = function (src) { | ||
| self.src = src; | ||
|
|
||
|
|
@@ -633,7 +670,7 @@ function methods () { | |
| var innerAspectRadio = res.width / res.height; | ||
|
|
||
| self.croperTarget = res.path; | ||
|
|
||
| if (innerAspectRadio < width / height) { | ||
| self.rectX = x; | ||
| self.baseWidth = width; | ||
|
|
@@ -651,7 +688,11 @@ function methods () { | |
| self.scaleWidth = self.baseWidth; | ||
| self.scaleHeight = self.baseHeight; | ||
|
|
||
| self.updateCanvas(); | ||
| if (!self.inRealTime) { | ||
| self.updateImage(true); | ||
| } else { | ||
| self.updateCanvas(); | ||
| } | ||
|
|
||
| isFunction(self.onImageLoad) && self.onImageLoad(self.ctx, self); | ||
| } | ||
|
|
@@ -677,6 +718,8 @@ function methods () { | |
| var args = [], len = arguments.length; | ||
| while ( len-- ) args[ len ] = arguments[ len ]; | ||
|
|
||
| self.updateCanvas(); | ||
|
|
||
| var ARG_TYPE = toString.call(args[0]); | ||
| var fn = args[args.length - 1]; | ||
|
|
||
|
|
@@ -760,7 +803,11 @@ function update () { | |
| var xMove, yMove; | ||
| // 计算单指移动的距离 | ||
| if (self.touchended) { | ||
| return self.updateCanvas() | ||
| if (!self.inRealTime) { | ||
| return self.updateImage() | ||
| } else { | ||
| return self.updateCanvas() | ||
| } | ||
| } | ||
| xMove = Math.round(touch.x - self.touchX0); | ||
| yMove = Math.round(touch.y - self.touchY0); | ||
|
|
@@ -770,7 +817,11 @@ function update () { | |
|
|
||
| self.outsideBound(imgLeft, imgTop); | ||
|
|
||
| self.updateCanvas(); | ||
| if (!self.inRealTime) { | ||
| self.updateImage(); | ||
| } else { | ||
| self.updateCanvas(); | ||
| } | ||
| }; | ||
|
|
||
| self.__twoTouchStart = function (touch0, touch1) { | ||
|
|
@@ -806,7 +857,11 @@ function update () { | |
|
|
||
| self.outsideBound(imgLeft, imgTop); | ||
|
|
||
| self.updateCanvas(); | ||
| if (!self.inRealTime) { | ||
| self.updateImage(); | ||
| } else { | ||
| self.updateCanvas(); | ||
| } | ||
| }; | ||
|
|
||
| self.__xtouchEnd = function () { | ||
|
|
||
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,14 @@ | ||
| <template name="we-cropper"> | ||
| <canvas | ||
| <view style='position:absolute;top:0px;width:{{width}}px;height:{{height}}px;background-color: rgba(0, 0, 0, 0.8);'> | ||
| <image wx:if="{{!inRealTime}}" src="{{cropperImageSrc}}" style="top:{{imageTop}}px;left:{{imageLeft}}px; width:{{imageWidth}}px;height:{{imageHeight}}px;position:absolute;"></image> | ||
| <canvas | ||
| class="cropper" | ||
| disable-scroll="true" | ||
| bindtouchstart="touchStart" | ||
| bindtouchstart="touchStart" | ||
| bindtouchmove="touchMove" | ||
| bindtouchend="touchEnd" | ||
| style="width:{{width}}px;height:{{height}}px;background-color: rgba(0, 0, 0, 0.8)" | ||
| style="width:{{width}}px;height:{{height}}px;" | ||
| canvas-id="{{id}}"> | ||
| </canvas> | ||
| </template> | ||
| </canvas> | ||
| </view> | ||
| </template> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,14 @@ | ||
| <template name="we-cropper"> | ||
| <canvas | ||
| <view style='position:absolute;top:0px;width:{{width}}px;height:{{height}}px;background-color: rgba(0, 0, 0, 0.8);'> | ||
| <image wx:if="{{!inRealTime}}" src="{{cropperImageSrc}}" style="top:{{imageTop}}px;left:{{imageLeft}}px; width:{{imageWidth}}px;height:{{imageHeight}}px;position:absolute;"></image> | ||
| <canvas | ||
| class="cropper" | ||
| disable-scroll="true" | ||
| bindtouchstart="touchStart" | ||
| bindtouchstart="touchStart" | ||
| bindtouchmove="touchMove" | ||
| bindtouchend="touchEnd" | ||
| style="width:{{width}}px;height:{{height}}px;background-color: rgba(0, 0, 0, 0.8)" | ||
| style="width:{{width}}px;height:{{height}}px;" | ||
| canvas-id="{{id}}"> | ||
| </canvas> | ||
| </template> | ||
| </canvas> | ||
| </view> | ||
| </template> |
Uh oh!
There was an error while loading. Please reload this page.