image-cropper
image-cropper copied to clipboard
Maximum call stack size exceeded;
对应工具或者iOS或者Andriod的版本号
微信版本号
代码截图
重现步骤 大量使用时有部分用户会产生错误:Maximum call stack size exceeded; 触发该错误的方法是”_cutTouchMove“ 期待的行为
实际的行为
请教下各位大佬该如何避免这个问题
同样的,我也会报这个错误
大佬有解决吗
感觉是_setData 这个方法吧,我把它注释掉了就好了
// 边距检测
if (this.data.cut_top > this.data.info.windowHeight - this.data.height) {
this.setData({
cut_top: this.data.info.windowHeight - this.data.height
});
}
问题就出在浮点数精度上,如果把 cut_Top(left) 和 windowHeight (width)打印出来会看到是这样的情况:
43.489 43.488999999999976
,
因而导致 touchmove ==> setData ==> cut_left watcher --> cutDetectionPosition ==> setData 的死循环。 @1977474741
感觉是_setData 这个方法吧,我把它注释掉了就好了
这个方法我搜了一下 没用到啊
图片看不到呀
------------------ 原始邮件 ------------------ 发件人: "Quentin"<[email protected]>; 发送时间: 2020年6月9日(星期二) 下午2:16 收件人: "wx-plugin/image-cropper"<[email protected]>; 抄送: "Subscribed"<[email protected]>; 主题: Re: [wx-plugin/image-cropper] Maximum call stack size exceeded; (#50)
感觉是_setData 这个方法吧,我把它注释掉了就好了
这个方法我搜了一下 没用到啊
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.
注释掉了也一样,有的IOS会,有的不会报错。
经过测试发现苹果12会出现 _cutDetectionPositionLeft这个方法注释掉就正常
_cutDetectionPositionLeft这个方法,我看到程序里面有调用,注释掉,那对功能自身没有影响吗?
_cutDetectionPositionLeft这个方法,我看到程序里面有调用,注释掉,那对功能自身没有影响吗?
暂时不知道呢,先正常再关注这个BUG有没有处理
有被使用,功能完整性可能会有影响
应该是浮点数精度问题,我装了个number-precision包处理了一下cut_left和cut_top这块的计算就没有报错了
应该是浮点数精度问题,我装了个number-precision包处理了一下cut_left和cut_top这块的计算就没有报错了
这个 方法贴出来看看
应该是浮点数精度问题,我装了个number-precision包处理了一下cut_left和cut_top这块的计算就没有报错了
这个 方法贴出来看看
/**
* 检测剪裁框位置是否在允许的范围内(屏幕内)
*/
_cutDetectionPosition() {
let _cutDetectionPositionTop = () => {
//检测上边距是否在范围内
if (this.data.cut_top < 0) {
this.setData({
cut_top: 0
});
}
if (this.data.cut_top > NP.minus(this.data.info.windowHeight, this.data.height)) {
this.setData({
cut_top: NP.minus(this.data.info.windowHeight, this.data.height)
});
}
},
_cutDetectionPositionLeft = () => {
//检测左边距是否在范围内
if (this.data.cut_left < 0) {
this.setData({
cut_left: 0
});
}
if (this.data.cut_left > NP.minus(this.data.info.windowWidth, this.data.width)) {
this.setData({
cut_left: NP.minus(this.data.info.windowWidth, this.data.width)
});
}
};
//裁剪框坐标处理(如果只写一个参数则另一个默认为0,都不写默认居中)
if (this.data.cut_top == null && this.data.cut_left == null) {
this._setCutCenter();
} else if (this.data.cut_top != null && this.data.cut_left != null) {
_cutDetectionPositionTop();
_cutDetectionPositionLeft();
} else if (this.data.cut_top != null && this.data.cut_left == null) {
_cutDetectionPositionTop();
this.setData({
cut_left: NP.minus(this.data.info.windowWidth, this.data.width) / 2
});
} else if (this.data.cut_top == null && this.data.cut_left != null) {
_cutDetectionPositionLeft();
this.setData({
cut_top: NP.minus(this.data.info.windowHeight, this.data.height) / 2
});
}
},
`'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
/*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. user ,medthor NP.strip(num) // strip a number to nearest right number NP.plus(num1, num2, num3, ...) // addition, num + num2 + num3, two numbers is required at least. NP.minus(num1, num2, num3, ...) // subtraction, num1 - num2 - num3 NP.times(num1, num2, num3, ...) // multiplication, num1 * num2 * num3 NP.divide(num1, num2, num3, ...) // division, num1 / num2 / num3 NP.round(num, ratio) // round a number based on ratio ***************************************************************************** / / global Reflect, Promise */
function __spreadArrays() { for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j]; return r; }
// @desc 解决浮动运算问题,避免小数点后产生多位数和计算精度损失。 // 问题示例:2.3 + 2.4 = 4.699999999999999,1.0 - 0.9 = 0.09999999999999998
// 把错误的数据转正 // strip(0.09999999999999998)=0.1
function strip(num, precision) { if (precision === void 0) { precision = 15; } return +parseFloat(Number(num).toPrecision(precision)); }
// Return digits length of a number // @param {number} num Input number
function digitLength(num) { // Get digit length of e var eSplit = num.toString().split(/[eE]/); var len = (eSplit[0].split('.')[1] || '').length - +(eSplit[1] || 0); return len > 0 ? len : 0; }
//把小数转成整数,支持科学计数法。如果是小数则放大成整数 //@param {number} num 输入数
function float2Fixed(num) { if (num.toString().indexOf('e') === -1) { return Number(num.toString().replace('.', '')); } var dLen = digitLength(num); return dLen > 0 ? strip(Number(num) * Math.pow(10, dLen)) : Number(num); }
//检测数字是否越界,如果越界给出提示 //@param {number} num 输入数
function checkBoundary(num) { if (_boundaryCheckingState) { if (num > Number.MAX_SAFE_INTEGER || num < Number.MIN_SAFE_INTEGER) { console.warn(num + " is beyond boundary when transfer to integer, the results may not be accurate"); } } }
//精确乘法
function times(num1, num2) { var others = []; for (var _i = 2; _i < arguments.length; _i++) { others[_i - 2] = arguments[_i]; } if (others.length > 0) { return times.apply(void 0, __spreadArrays([times(num1, num2), others[0]], others.slice(1))); } var num1Changed = float2Fixed(num1); var num2Changed = float2Fixed(num2); var baseNum = digitLength(num1) + digitLength(num2); var leftValue = num1Changed * num2Changed; checkBoundary(leftValue); return leftValue / Math.pow(10, baseNum); }
// 精确加法
function plus(num1, num2) { var others = []; for (var _i = 2; _i < arguments.length; _i++) { others[_i - 2] = arguments[_i]; } if (others.length > 0) { return plus.apply(void 0, __spreadArrays([plus(num1, num2), others[0]], others.slice(1))); } var baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2))); return (times(num1, baseNum) + times(num2, baseNum)) / baseNum; }
//精确减法
function minus(num1, num2) { var others = []; for (var _i = 2; _i < arguments.length; _i++) { others[_i - 2] = arguments[_i]; } if (others.length > 0) { return minus.apply(void 0, __spreadArrays([minus(num1, num2), others[0]], others.slice(1))); } var baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2))); return (times(num1, baseNum) - times(num2, baseNum)) / baseNum; }
//精确除法
function divide(num1, num2) { var others = []; for (var _i = 2; _i < arguments.length; _i++) { others[_i - 2] = arguments[_i]; } if (others.length > 0) { return divide.apply(void 0, __spreadArrays([divide(num1, num2), others[0]], others.slice(1))); } var num1Changed = float2Fixed(num1); var num2Changed = float2Fixed(num2); checkBoundary(num1Changed); checkBoundary(num2Changed); // fix: 类似 10 ** -4 为 0.00009999999999999999,strip 修正 return times(num1Changed / num2Changed, strip(Math.pow(10, digitLength(num2) - digitLength(num1)))); }
//四舍五入
function round(num, ratio) { var base = Math.pow(10, ratio); return divide(Math.round(times(num, base)), base); } var _boundaryCheckingState = true;
// 是否进行边界检查,默认开启 // @param flag 标记开关,true 为开启,false 为关闭,默认为 true
function enableBoundaryChecking(flag) { if (flag === void 0) { flag = true; } _boundaryCheckingState = flag; } var index = { strip: strip, plus: plus, minus: minus, times: times, divide: divide, round: round, digitLength: digitLength, float2Fixed: float2Fixed, enableBoundaryChecking: enableBoundaryChecking, }; exports.strip = strip; exports.plus = plus; exports.minus = minus; exports.times = times; exports.divide = divide; exports.round = round; exports.digitLength = digitLength; exports.float2Fixed = float2Fixed; exports.enableBoundaryChecking = enableBoundaryChecking; exports['default'] = index;`
number-precision.js