wx-canvas-2d
Version:
微信小程序 canvas-2d 绘图工具,支持按需引用,支持内容配置,支持功能扩展,适配各种机型,超轻量,超易用,无需学习,直接上手。
1 lines • 2.42 kB
JavaScript
;function _toConsumableArray(t){return _arrayWithoutHoles(t)||_iterableToArray(t)||_unsupportedIterableToArray(t)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(t,r){var e;if(t)return"string"==typeof t?_arrayLikeToArray(t,r):"Map"===(e="Object"===(e=Object.prototype.toString.call(t).slice(8,-1))&&t.constructor?t.constructor.name:e)||"Set"===e?Array.from(t):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?_arrayLikeToArray(t,r):void 0}function _iterableToArray(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}function _arrayWithoutHoles(t){if(Array.isArray(t))return _arrayLikeToArray(t)}function _arrayLikeToArray(t,r){(null==r||r>t.length)&&(r=t.length);for(var e=0,i=new Array(r);e<r;e++)i[e]=t[e];return i}module.exports={name:"Image",handler:function(){var g=this,i=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{};return new Promise(function(n,r){var t=i.url,e=void 0===t?"":t,t=i.x,h=void 0===t?0:t,t=i.y,l=void 0===t?0:t,t=i.width,d=void 0===t?0:t,t=i.height,c=void 0===t?0:t,t=i.mode,u=void 0===t?"scaleToFill":t,t=i.radius,s=void 0===t?0:t,y=g.canvas.createImage();y.src=e,y.onload=function(){wx.getImageInfo({src:e,success:function(t){var r=t.width,e=t.height,i=d/c,o=1,a=1,i=("aspectFit"===u?(o=t.width/t.height<i?d/t.width*t.height/c:1,a=t.width/t.height>i?c/t.height*t.width/d:1):"aspectFill"===u&&(o=t.width/t.height>i?d/t.width*t.height/c:1,a=t.width/t.height<i?c/t.height*t.width/d:1),{scaleToFill:[0,0,r,e],aspectFit:[(t.width-t.width*o)/2,(t.height-t.height*a)/2,t.width*o,t.height*a],aspectFill:[(t.width-t.width*o)/2,(t.height-t.height*a)/2,t.width*o,t.height*a],widthFix:[],top:[(r-d)/2,0,d,c],bottom:[(r-d)/2,e-c,d,c],center:[(r-d)/2,(e-c)/2,d,c],left:[0,(e-c)/2,d,c],right:[r-d,(e-c)/2,d,c],"top left":[0,0,d,c],"top right":[r-d,0,d,c],"bottom left":[0,e-c,d,c],"bottom right":[r-d,e-c,d,c]});s&&(g.ctx.save(),g.drawRectPath({x:h,y:l,width:d,height:c,radius:s}),g.ctx.clip()),(o=g.ctx).drawImage.apply(o,[y].concat(_toConsumableArray(i[u]||[]),[g.xDpr(h)||0,g.xDpr(l)||0,g.xDpr(d||t.width),g.xDpr(c||t.height)])),s&&g.ctx.restore(),n()},fail:function(t){r(t)}})},y.onerror=function(t){r(t)}})}};