|
@@ -36,7 +36,8 @@ ImageLoader.prototype.load = function (urls, config, onSuccess, onError) {
|
|
|
onError = onError || (function (error) {
|
|
|
console.log(error)
|
|
|
})
|
|
|
- urls = (urls != undefined && Array.isArray(urls)) ? urls : []
|
|
|
+ urls = Array.isArray(urls) ? urls : []
|
|
|
+ config = Array.isArray(config) ? config : []
|
|
|
let promises = []
|
|
|
let images = []
|
|
|
let self = this
|
|
@@ -49,7 +50,7 @@ ImageLoader.prototype.load = function (urls, config, onSuccess, onError) {
|
|
|
promises.push(new Promise((resolve, reject) => {
|
|
|
if (self.contain(image.index, config)) {
|
|
|
image.onload = function () {
|
|
|
- image.loaded = true
|
|
|
+ this.loaded = true
|
|
|
resolve()
|
|
|
}
|
|
|
image.onerror = function () {
|
|
@@ -69,12 +70,37 @@ ImageLoader.prototype.load = function (urls, config, onSuccess, onError) {
|
|
|
}
|
|
|
|
|
|
Promise.all(promises).then(() => {
|
|
|
+ self.checkSize(images)
|
|
|
onSuccess(images)
|
|
|
}).catch(error => {
|
|
|
onError(error)
|
|
|
})
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * 遍历所有加载图片,根据宽高相互比较,判断是否大小图
|
|
|
+ * @param {array} images
|
|
|
+ */
|
|
|
+ImageLoader.prototype.checkSize = function (images) {
|
|
|
+ //let maxWidth = 0
|
|
|
+ for (let i = 0; i < images.length; i++) {
|
|
|
+ //宽度小于等于高度,自动判断为小图
|
|
|
+ if (images[i].width <= images[i].height) {
|
|
|
+ images[i].wide = false
|
|
|
+ } else {
|
|
|
+ //maxWidth = Math.max(maxWidth, images[i].width)
|
|
|
+ images[i].wide = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // for (let i = 0; i < images.length; i++) {
|
|
|
+ // if (images[i].width > images[i].height) {
|
|
|
+ // //宽度大于高度,但是不到所有大图最大宽度的85%,也判定为小图
|
|
|
+ // let diff = images[i].width / maxWidth
|
|
|
+ // images[i].wide = diff >= 0.85
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+}
|
|
|
+
|
|
|
/**
|
|
|
* 判断指定序号的图片是否在拼接规则中存在,默认返回true
|
|
|
*
|
|
@@ -82,7 +108,7 @@ ImageLoader.prototype.load = function (urls, config, onSuccess, onError) {
|
|
|
* @param {array} config
|
|
|
*/
|
|
|
ImageLoader.prototype.contain = function (number, config) {
|
|
|
- if (config != undefined && config.length > 0) {
|
|
|
+ if (config.length > 0) {
|
|
|
let find = false
|
|
|
for (let i = 0; i < config.length; i++) {
|
|
|
if (config[i].i == number) {
|
|
@@ -100,7 +126,7 @@ ImageLoader.prototype.contain = function (number, config) {
|
|
|
/**
|
|
|
* 使用指定画布,按照云阅卷拼接规则,加载图片并组合
|
|
|
*
|
|
|
- * @param {array} images
|
|
|
+ * @param {array} urls
|
|
|
* @param {canvas} canvas
|
|
|
* @param {array} config
|
|
|
* @param {function} onSuccess
|
|
@@ -112,18 +138,42 @@ ImageLoader.prototype.combine = function (urls, canvas, config, onSuccess, onErr
|
|
|
onError = onError || (function (error) {
|
|
|
console.log(error)
|
|
|
})
|
|
|
+ urls = Array.isArray(urls) ? urls : []
|
|
|
+ config = Array.isArray(config) ? config : []
|
|
|
this.load(urls, config, function (images) {
|
|
|
- config = (config != undefined && Array.isArray(config)) ? config : []
|
|
|
if (config.length == 0) {
|
|
|
//不指定拼接配置时,默认按顺序显示所有图片
|
|
|
+ //区分大小图,小图原样显示,大图默认一分为二
|
|
|
for (let i = 0; i < images.length; i++) {
|
|
|
- config.push({
|
|
|
- i: i + 1,
|
|
|
- x: 0,
|
|
|
- y: 0,
|
|
|
- w: 0,
|
|
|
- h: 0
|
|
|
- })
|
|
|
+ let image = images[i]
|
|
|
+ if (image.loaded === true) {
|
|
|
+ if (image.wide === true) {
|
|
|
+ //大图一分为二
|
|
|
+ config.push({
|
|
|
+ i: i + 1,
|
|
|
+ x: 0,
|
|
|
+ y: 0,
|
|
|
+ w: image.width * 0.55,
|
|
|
+ h: image.height
|
|
|
+ })
|
|
|
+ config.push({
|
|
|
+ i: i + 1,
|
|
|
+ x: image.width * 0.45,
|
|
|
+ y: 0,
|
|
|
+ w: image.width * 0.55,
|
|
|
+ h: image.height
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ //小图原样显示
|
|
|
+ config.push({
|
|
|
+ i: i + 1,
|
|
|
+ x: 0,
|
|
|
+ y: 0,
|
|
|
+ w: image.width,
|
|
|
+ h: image.height
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
let maxWidth = 0
|
|
@@ -131,7 +181,7 @@ ImageLoader.prototype.combine = function (urls, canvas, config, onSuccess, onErr
|
|
|
for (let i = 0; i < config.length; i++) {
|
|
|
let image = images[config[i] - 1]
|
|
|
//必须图片存在且正常加载配置才生效
|
|
|
- if (image != undefined && image.loaded === true) {
|
|
|
+ if (image.loaded === true) {
|
|
|
//计算最大宽度与合计高度
|
|
|
if (config[i].w <= 0) {
|
|
|
config[i].w = image.width
|
|
@@ -186,7 +236,7 @@ ImageLoader.prototype.merge = function (urls, canvas, config, onSuccess, onError
|
|
|
}
|
|
|
image.config = array
|
|
|
image.src = canvas.toDataURL()
|
|
|
- })
|
|
|
+ }, null)
|
|
|
}
|
|
|
|
|
|
/**
|