-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(h5 component): 支持canvasgetimagedata canvasputimage api
- Loading branch information
Showing
8 changed files
with
196 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module.exports = { | ||
extends: ['standard', 'standard-jsx'], | ||
env: { | ||
browser: true, | ||
node: true, | ||
es6: true, | ||
jest: true | ||
}, | ||
globals: { | ||
wx: true, | ||
my: true, | ||
swan: true, | ||
tt: true, | ||
getApp: true, | ||
__wxRoute: true, | ||
getCurrentPages: true, | ||
requirePlugin: true | ||
}, | ||
rules: { | ||
'no-unused-expressions': 0, | ||
'no-useless-constructor': 0, | ||
'no-undef': 2 | ||
}, | ||
settings: { | ||
react: { | ||
pragma: 'Nerv' | ||
} | ||
}, | ||
parser: 'babel-eslint' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,46 @@ | ||
export default () => {} | ||
import { findDOMNode } from 'nervjs' | ||
|
||
/** | ||
* @typedef {Object} Param | ||
* @property {String} canvasId 画布标识,传入 <canvas> 组件的 canvas-id 属性。 | ||
* @property {Number} x 将要被提取的图像数据矩形区域的左上角横坐标 | ||
* @property {Number} y 将要被提取的图像数据矩形区域的左上角纵坐标 | ||
* @property {Number} width 将要被提取的图像数据矩形区域的宽度 | ||
* @property {Number} height 将要被提取的图像数据矩形区域的高度 | ||
* @property {Function} [success] 接口调用成功的回调函数 | ||
* @property {Function} [fail] 接口调用失败的回调函数 | ||
* @property {Function} [complete] 接口调用结束的回调函数(调用成功、失败都会执行) | ||
*/ | ||
|
||
/** | ||
* 获取 canvas 区域隐含的像素数据。 | ||
* @param {Param} object 参数 | ||
* @param {Object} componentInstance | ||
*/ | ||
const canvasGetImageData = ({ canvasId, success, fail, complete, x, y, width, height }, componentInstance=document.body) => { | ||
const dom = findDOMNode(componentInstance) | ||
|
||
/** @type {HTMLCanvasElement} */ | ||
const canvas = dom.querySelector(`[canvasId=${canvasId}]`); | ||
try { | ||
const ctx = canvas.getContext('2d') | ||
const data = ctx.getImageData(x, y, width, height) | ||
const res = { | ||
width, | ||
height, | ||
data | ||
} | ||
success && success(res) | ||
complete && complete() | ||
return Promise.resolve(res) | ||
} catch (e) { | ||
const res = { | ||
errMsg: e.message | ||
} | ||
fail && fail(res) | ||
complete && complete() | ||
return Promise.reject(res) | ||
} | ||
} | ||
|
||
export default canvasGetImageData |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,46 @@ | ||
export default () => {} | ||
import { findDOMNode } from 'nervjs' | ||
|
||
/** | ||
* @typedef {Object} Param | ||
* @property {String} canvasId 是 画布标识,传入 <canvas> 组件的 canvas-id 属性。 | ||
* @property {Uint8ClampedArray} data 是 图像像素点数据,一维数组,每四项表示一个像素点的 rgba | ||
* @property {Number} x 是 源图像数据在目标画布中的位置偏移量(x 轴方向的偏移量) | ||
* @property {Number} y 是 源图像数据在目标画布中的位置偏移量(y 轴方向的偏移量) | ||
* @property {Number} width 是 源图像数据矩形区域的宽度 | ||
* @property {Number} height 是 源图像数据矩形区域的高度 | ||
* @property {Function} [success] 接口调用成功的回调函数 | ||
* @property {Function} [fail] 接口调用失败的回调函数 | ||
* @property {Function} [complete] 接口调用结束的回调函数(调用成功、失败都会执行) | ||
*/ | ||
|
||
/** | ||
* 将像素数据绘制到画布。在自定义组件下,第二个参数传入自定义组件实例 this,以操作组件内 <canvas> 组件 | ||
* @param {Param} object 参数 | ||
* @param {Object} componentInstance 在自定义组件下,当前组件实例的this,以操作组件内 <canvas> 组件 | ||
*/ | ||
const canvasPutImageData = ({ canvasId, data, x, y, success, fail, complete }, componentInstance=document.body) => { | ||
const dom = findDOMNode(componentInstance) | ||
|
||
/** @type {HTMLCanvasElement} */ | ||
const canvas = dom.querySelector(`[canvasId=${canvasId}]`); | ||
try { | ||
const ctx = canvas.getContext('2d') | ||
|
||
ctx.putImageData(data, x, y) | ||
const res = { | ||
errMsg: 'canvasPutImageData:ok' | ||
} | ||
success && success(res) | ||
complete && complete() | ||
return Promise.resolve(res) | ||
} catch (e) { | ||
const res = { | ||
errMsg: e.message | ||
} | ||
fail && fail(res) | ||
complete && complete() | ||
return Promise.reject(res) | ||
} | ||
} | ||
|
||
export default canvasPutImageData |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters