wot-design
Version:
Mobile UI components built on vue.js
1,115 lines (995 loc) • 32 kB
JavaScript
module.exports =
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
/******/ }
/******/ };
/******/
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function(exports) {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/
/******/ // create a fake namespace object
/******/ // mode & 1: value is a module id, require it
/******/ // mode & 2: merge all properties of value into the ns
/******/ // mode & 4: return value when already ns object
/******/ // mode & 8|1: behave like require
/******/ __webpack_require__.t = function(value, mode) {
/******/ if(mode & 1) value = __webpack_require__(value);
/******/ if(mode & 8) return value;
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/ var ns = Object.create(null);
/******/ __webpack_require__.r(ns);
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
/******/ return ns;
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 81);
/******/ })
/************************************************************************/
/******/ ({
/***/ 0:
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return normalizeComponent; });
/* globals __VUE_SSR_CONTEXT__ */
// IMPORTANT: Do NOT use ES2015 features in this file (except for modules).
// This module is a runtime utility for cleaner component module output and will
// be included in the final webpack user bundle.
function normalizeComponent (
scriptExports,
render,
staticRenderFns,
functionalTemplate,
injectStyles,
scopeId,
moduleIdentifier, /* server only */
shadowMode /* vue-cli only */
) {
// Vue.extend constructor export interop
var options = typeof scriptExports === 'function'
? scriptExports.options
: scriptExports
// render functions
if (render) {
options.render = render
options.staticRenderFns = staticRenderFns
options._compiled = true
}
// functional template
if (functionalTemplate) {
options.functional = true
}
// scopedId
if (scopeId) {
options._scopeId = 'data-v-' + scopeId
}
var hook
if (moduleIdentifier) { // server build
hook = function (context) {
// 2.3 injection
context =
context || // cached call
(this.$vnode && this.$vnode.ssrContext) || // stateful
(this.parent && this.parent.$vnode && this.parent.$vnode.ssrContext) // functional
// 2.2 with runInNewContext: true
if (!context && typeof __VUE_SSR_CONTEXT__ !== 'undefined') {
context = __VUE_SSR_CONTEXT__
}
// inject component styles
if (injectStyles) {
injectStyles.call(this, context)
}
// register component module identifier for async chunk inferrence
if (context && context._registeredComponents) {
context._registeredComponents.add(moduleIdentifier)
}
}
// used by ssr in case component is cached and beforeCreate
// never gets called
options._ssrRegister = hook
} else if (injectStyles) {
hook = shadowMode
? function () { injectStyles.call(this, this.$root.$options.shadowRoot) }
: injectStyles
}
if (hook) {
if (options.functional) {
// for template-only hot-reload because in that case the render fn doesn't
// go through the normalizer
options._injectStyles = hook
// register for functioal component in vue file
var originalRender = options.render
options.render = function renderWithStyleInjection (h, context) {
hook.call(context)
return originalRender(h, context)
}
} else {
// inject component registration as beforeCreate hook
var existing = options.beforeCreate
options.beforeCreate = existing
? [].concat(existing, hook)
: [hook]
}
}
return {
exports: scriptExports,
options: options
}
}
/***/ }),
/***/ 12:
/***/ (function(module, exports) {
module.exports = require("wot-design/lib/button");
/***/ }),
/***/ 4:
/***/ (function(module, exports) {
module.exports = require("wot-design/lib/mixins/locale");
/***/ }),
/***/ 81:
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
__webpack_require__.r(__webpack_exports__);
// CONCATENATED MODULE: ./node_modules/vue-loader/lib/loaders/templateLoader.js??vue-loader-options!./node_modules/vue-loader/lib??vue-loader-options!./packages/img-cropper/src/main.vue?vue&type=template&id=048b6f54&
var render = function() {
var _vm = this
var _h = _vm.$createElement
var _c = _vm._self._c || _h
return _vm.show
? _c(
"div",
{
ref: "cropper",
staticClass: "wd-img-cropper",
style: { width: _vm.rootWidth, height: _vm.rootHeight }
},
[
_c("div", { staticClass: "wd-img-cropper__wrapper" }, [
_c("div", { staticClass: "wd-img-cropper__cut" }, [
_c("div", {
class: [
{ "wd-img-cropper__cut--top": true },
{ "is-hightlight": !_vm.IS_TOUCH_END }
],
style: { height: _vm.cutTop + "px" }
}),
_vm._v(" "),
_c("div", { staticClass: "wd-img-cropper__cut--middle" }, [
_c("div", {
class: [
{ "wd-img-cropper__cut--left": true },
{ "is-hightlight": !_vm.IS_TOUCH_END }
],
style: {
width: _vm.cutLeft + "px",
height: _vm.cutWidth + "px"
}
}),
_vm._v(" "),
_c(
"div",
{
staticClass: "wd-img-cropper__cut--body",
style: {
width: _vm.cutWidth + "px",
height: _vm.cutHeight + "px"
}
},
[
_c("div", { staticClass: "is-gridlines-x" }),
_vm._v(" "),
_c("div", { staticClass: "is-gridlines-y" }),
_vm._v(" "),
_c("div", { staticClass: "is-left-top" }),
_vm._v(" "),
_c("div", { staticClass: "is-left-bottom" }),
_vm._v(" "),
_c("div", { staticClass: "is-right-top" }),
_vm._v(" "),
_c("div", { staticClass: "is-right-bottom" })
]
),
_vm._v(" "),
_c("div", {
class: [
{ "wd-img-cropper__cut--right": true },
{ "is-hightlight": !_vm.IS_TOUCH_END }
]
})
]),
_vm._v(" "),
_c("div", {
class: [
{ "wd-img-cropper__cut--bottom": true },
{ "is-hightlight": !_vm.IS_TOUCH_END }
]
})
]),
_vm._v(" "),
_c("img", {
staticClass: "wd-img-cropper__img",
style: {
width: _vm.picWidth ? _vm.picWidth + "px" : "auto",
height: _vm.picHeight ? _vm.picHeight + "px" : "auto",
transform:
"translate3d(" +
(_vm.imgLeft - _vm.picWidth / 2) +
"px, " +
(_vm.imgTop - _vm.picHeight / 2) +
"px, 0) scale(" +
_vm.imgScale +
") rotate(" +
_vm.imgAngle +
"deg)",
"transition-duration": (_vm.isAnimation ? 0.3 : 0) + "s"
},
attrs: { id: "wd-cropper-img", src: _vm.imgSrc },
on: {
touchstart: _vm.handleImgTouchStart,
touchmove: _vm.handleImgTouchMove,
touchend: _vm.handleImgTouchEnd
}
})
]),
_vm._v(" "),
_c("canvas", {
ref: "canvas",
staticClass: "wd-img-cropper__canvas",
attrs: {
"disable-scroll": "true",
width: _vm.canvasWidth * _vm.exportScale,
height: _vm.canvasHeight * _vm.exportScale
}
}),
_vm._v(" "),
_c("div", { staticClass: "wd-img-cropper__footer" }, [
!_vm.disabledRotate
? _c("i", {
staticClass: "wd-icon-rotate wd-img-cropper__footer--rotate",
on: { click: _vm.handleRotate }
})
: _vm._e(),
_vm._v(" "),
_c(
"div",
{ staticClass: "wd-img-cropper__footer--button" },
[
_c(
"div",
{ staticClass: "is-cancel", on: { click: _vm.handleCancel } },
[
_vm._v(
"\n " +
_vm._s(
_vm.cancelButtonText || _vm.t("wd.imgCropper.cancel")
) +
"\n "
)
]
),
_vm._v(" "),
_c(
"wd-button",
{
staticClass: "is-confirm",
attrs: { size: "small" },
on: { click: _vm.handleConfirm }
},
[
_vm._v(
"\n " +
_vm._s(
_vm.confirmButtonText ||
_vm.t("wd.imgCropper.confirm")
) +
"\n "
)
]
)
],
1
)
])
]
)
: _vm._e()
}
var staticRenderFns = []
render._withStripped = true
// CONCATENATED MODULE: ./packages/img-cropper/src/main.vue?vue&type=template&id=048b6f54&
// EXTERNAL MODULE: external "wot-design/lib/mixins/locale"
var locale_ = __webpack_require__(4);
var locale_default = /*#__PURE__*/__webpack_require__.n(locale_);
// EXTERNAL MODULE: external "wot-design/lib/button"
var button_ = __webpack_require__(12);
var button_default = /*#__PURE__*/__webpack_require__.n(button_);
// CONCATENATED MODULE: ./node_modules/babel-loader/lib!./node_modules/vue-loader/lib??vue-loader-options!./packages/img-cropper/src/main.vue?vue&type=script&lang=js&
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
// 延时动画设置
var CHANGE_TIME = null; // 移动节流
var MOVE_THROTTLE = null; // 节流标志
var MOVE_THROTTLE_FLAG = true; // 图片设置尺寸,此值不变(记录最初设定的尺寸)
var INIT_IMGWIDTH = null; // 图片设置尺寸,此值不变(记录最初设定的尺寸)
var INIT_IMGHEIGHT = null;
var DISTANCE_LEFT = 0;
var DISTANCE_TOP = 0; // 顶部裁剪框占比
var TOP_PERCENT = 0.85;
/* harmony default export */ var mainvue_type_script_lang_js_ = ({
name: 'WdImgCropper',
mixins: [locale_default.a],
components: {
WdButton: button_default.a
},
data: function data() {
return {
rootWidth: window.screen.width,
rootHeight: window.screen.height,
picWidth: this.imgWidth,
picHeight: this.imgHeight,
isAnimation: false,
imgAngle: 0,
show: false,
// 裁剪框的宽高
cutWidth: 0,
cutHeight: 0,
offset: 20,
// 裁剪框的距顶距左
cutLeft: 0,
cutTop: 0,
// canvas最终成像宽高
canvasWidth: '',
canvasHeight: '',
canvasScale: 2,
// 当前缩放大小
imgScale: 1,
// 图片中心轴点距左的距离
imgLeft: window.screen.availWidth / 2,
imgTop: window.screen.availHeight / 2 * TOP_PERCENT,
// 是否移动中设置 同时控制背景颜色是否高亮
IS_TOUCH_END: true,
// 记录移动中的双指位置 [0][1]分别代表两根手指 [1]做待用参数
movingPosRecord: [{
x: '',
y: ''
}, {
x: '',
y: ''
}],
// 双指缩放时 两个坐标点斜边长度
fingerDistance: ''
};
},
props: {
value: Boolean,
cancelButtonText: String,
confirmButtonText: String,
// 是否禁用旋转
disabledRotate: Boolean,
// 图片源路径
imgSrc: String,
// 图片宽
imgWidth: null,
// 图片高
imgHeight: null,
// 最大缩放
maxScale: {
type: Number,
default: 3
},
// 输出倍数
exportScale: {
type: Number,
default: 2
},
/** canvas绘图参数 start **/
// canvasToTempFilePath —— fileType
fileType: {
type: String,
default: 'png'
},
// canvasToTempFilePath —— quality
quality: {
type: Number,
default: 1
},
filename: {
type: String,
default: 'filename'
}
},
watch: {
value: {
handler: function handler(val) {
if (val !== undefined) {
this.show = val;
}
},
immediate: true
},
show: {
handler: function handler(val) {
if (val) {
this.open();
}
},
immediate: true
},
imgSrc: {
handler: function handler(val) {
val && this.loadImg();
},
immediate: true
},
imgAngle: function imgAngle(val) {
if (val % 90) {
this.imgAngle = Math.round(val / 90) * 90;
}
},
isAnimation: function isAnimation(val) {
var _this = this;
// 开启过渡300毫秒之后自动关闭
clearTimeout(CHANGE_TIME);
if (val) {
CHANGE_TIME = setTimeout(function () {
_this.isAnimation = false;
}, 300);
}
}
},
methods: {
/**
* @description 打开图片裁剪窗口
*/
open: function open() {
var _this2 = this;
this.$nextTick(function () {
_this2.resize();
INIT_IMGWIDTH = _this2.imgWidth;
INIT_IMGHEIGHT = _this2.imgHeight;
var cutSize = _this2.rootWidth - _this2.offset * 2;
_this2.cutWidth = cutSize;
_this2.cutHeight = cutSize;
_this2.cutTop = (_this2.rootHeight * TOP_PERCENT - cutSize) / 2;
_this2.cutLeft = _this2.offset;
_this2.canvasScale = _this2.exportScale;
_this2.canvasHeight = cutSize;
_this2.canvasWidth = cutSize; // 根据开发者设置的图片目标尺寸计算实际尺寸
_this2.initImageSize(); // 初始化canvas
_this2.initCanvas(); // 加载图片
_this2.imgSrc && _this2.loadImg();
});
},
/**
* @description 对外暴露:控制旋转角度
* @param {Number} angle 角度
*/
setRoate: function setRoate(angle) {
if (!angle || this.disabledRotate) return;
this.isAnimation = true;
this.imgAngle = angle; // 设置旋转后需要判定旋转后宽高是否不符合贴边的标准
this.detectImgPosIsEdge();
},
resize: function resize() {
var rect = this.$refs.cropper.getBoundingClientRect();
this.rootWidth = rect.width;
this.rootHeight = rect.height;
},
/**
* @description 对外暴露:初始化图片的大小和角度以及距离
*/
resetImg: function resetImg() {
this.imgScale = 1;
this.imgAngle = 0;
this.imgLeft = this.rootWidth / 2;
this.imgTop = this.rootHeight / 2 * TOP_PERCENT;
},
/**
* @description 加载图片资源文件,并初始化裁剪框内图片信息
*/
loadImg: function loadImg() {
var _this3 = this;
if (!this.imgSrc) return;
var image = new Image();
image.onload = function () {
_this3.imgInfo = {
width: image.width,
height: image.height
};
_this3.computeImgSize();
_this3.resetImg();
};
image.src = this.imgSrc;
},
/**
* @description 设置图片尺寸,使其有一边小于裁剪框尺寸
* 1、图片宽或高 小于裁剪框,自动放大至一边与高平齐
* 2、图片宽或高 大于裁剪框,自动缩小至一边与高平齐
*/
computeImgSize: function computeImgSize() {
var picWidth = this.picWidth,
picHeight = this.picHeight,
imgInfo = this.imgInfo,
cutWidth = this.cutWidth,
cutHeight = this.cutHeight;
if (!INIT_IMGHEIGHT && !INIT_IMGWIDTH) {
// 没有设置宽高,写入图片的真实宽高
picWidth = imgInfo.width;
picHeight = imgInfo.height;
/**
* 设 a = picWidth; b = picHeight; x = cutWidth; y = cutHeight
* 共有三种宽高比:1、a/b > x/y 2、a/b < x/y 3、a/b = x/y
* 1、已知 b = y => a = a/b*y
* 2、已知 a = x => b = b/a*x
* 3、可用上方任意公式
*/
if (picWidth / picHeight > cutWidth / cutHeight) {
picHeight = cutHeight;
picWidth = imgInfo.width / imgInfo.height * cutHeight;
} else {
picWidth = cutWidth;
picHeight = imgInfo.height / imgInfo.width * cutWidth;
}
} else if (INIT_IMGHEIGHT && !INIT_IMGWIDTH) {
picWidth = imgInfo.width / imgInfo.height * INIT_IMGHEIGHT;
} else if (!INIT_IMGHEIGHT && INIT_IMGWIDTH || INIT_IMGHEIGHT && INIT_IMGWIDTH) {
picHeight = imgInfo.height / imgInfo.width * INIT_IMGWIDTH;
}
this.picWidth = picWidth;
this.picHeight = picHeight;
},
/**
* @description canvas 初始化
*/
initCanvas: function initCanvas() {
var _this4 = this;
this.ctx = null;
this.canvas = null;
if (!this.ctx) {
this.$nextTick(function () {
_this4.canvas = _this4.$refs.canvas;
_this4.ctx = _this4.canvas.getContext('2d');
});
}
},
/**
* @description 图片初始化,处理宽高特殊单位
*/
initImageSize: function initImageSize() {
// 处理宽高特殊单位 %>px
if (INIT_IMGWIDTH && typeof INIT_IMGWIDTH === 'string' && INIT_IMGWIDTH.indexOf('%') !== -1) {
var width = INIT_IMGWIDTH.replace('%', '');
INIT_IMGWIDTH = this.picWidth = this.rootWidth / 100 * width;
} else if (INIT_IMGWIDTH && typeof INIT_IMGWIDTH === 'number') {
this.picWidth = INIT_IMGWIDTH;
}
if (INIT_IMGHEIGHT && typeof INIT_IMGHEIGHT === 'string' && INIT_IMGHEIGHT.indexOf('%') !== -1) {
var height = this.picHeight.replace('%', '');
INIT_IMGHEIGHT = this.picHeight = this.rootHeight / 100 * height;
} else if (INIT_IMGHEIGHT && typeof INIT_IMGHEIGHT === 'number') {
this.picWidth = INIT_IMGHEIGHT;
}
},
/**
* @description 图片拖动边缘检测:检测移动或缩放时 是否触碰到图片边缘位置
*/
detectImgPosIsEdge: function detectImgPosIsEdge(scale) {
var cutLeft = this.cutLeft,
cutTop = this.cutTop,
cutWidth = this.cutWidth,
cutHeight = this.cutHeight,
imgAngle = this.imgAngle;
var imgScale = scale || this.imgScale;
var picWidth = this.picWidth,
picHeight = this.picHeight; // 翻转后宽高切换
if (imgAngle / 90 % 2) {
picWidth = this.picHeight;
picHeight = this.picWidth;
} // 左
var imgLeft = picWidth * imgScale / 2 + cutLeft >= DISTANCE_LEFT ? DISTANCE_LEFT : picWidth * imgScale / 2 + cutLeft; // 右
imgLeft = cutLeft + cutWidth - picWidth * imgScale / 2 <= imgLeft ? imgLeft : cutLeft + cutWidth - picWidth * imgScale / 2; // 上
var imgTop = picHeight * imgScale / 2 + cutTop >= DISTANCE_TOP ? DISTANCE_TOP : picHeight * imgScale / 2 + cutTop; // 下
imgTop = cutTop + cutHeight - picHeight * imgScale / 2 <= imgTop ? imgTop : cutTop + cutHeight - picHeight * imgScale / 2;
this.imgLeft = imgLeft;
this.imgTop = imgTop;
this.imgScale = imgScale;
},
/**
* @description 缩放边缘检测:检测移动或缩放时 是否触碰到图片边缘位置
*/
detectImgScaleIsEdge: function detectImgScaleIsEdge() {
var cutHeight = this.cutHeight,
cutWidth = this.cutWidth,
imgAngle = this.imgAngle;
var picWidth = this.picWidth,
picHeight = this.picHeight,
imgScale = this.imgScale; // 翻转后宽高切换
if (imgAngle / 90 % 2) {
picWidth = this.picHeight;
picHeight = this.picWidth;
}
if (picWidth * imgScale < cutWidth) {
imgScale = cutWidth / picWidth;
}
if (picHeight * imgScale < cutHeight) {
imgScale = cutHeight / picHeight;
}
this.detectImgPosIsEdge(imgScale);
},
/**
* @description 节流
*/
throttle: function throttle() {
if (/(Android)/i.test(navigator.userAgent)) {
clearTimeout(MOVE_THROTTLE);
MOVE_THROTTLE = setTimeout(function () {
MOVE_THROTTLE_FLAG = true;
}, 1000 / 40);
} else {
!MOVE_THROTTLE_FLAG && (MOVE_THROTTLE_FLAG = true);
}
},
/**
* @description {图片区} 开始拖动
*/
handleImgTouchStart: function handleImgTouchStart(event) {
// 如果处于在拖动中,背景为淡色展示全部,拖动结束则为 0.85 透明度
this.IS_TOUCH_END = false;
if (event.touches.length === 1) {
// 单指拖动
this.movingPosRecord[0] = {
x: event.touches[0].clientX - this.imgLeft,
y: event.touches[0].clientY - this.imgTop
};
} else {
// 以两指为坐标点 做直角三角形 a2 + b2 = c2
var width = Math.abs(event.touches[1].clientX - event.touches[0].clientX);
var height = Math.abs(event.touches[1].clientY - event.touches[0].clientY);
this.fingerDistance = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2));
}
},
/**
* @description {图片区} 拖动中
*/
handleImgTouchMove: function handleImgTouchMove(event) {
if (this.IS_TOUCH_END || !MOVE_THROTTLE_FLAG) return; // 节流
this.throttle();
if (event.touches.length === 1) {
// 单指拖动
var _this$movingPosRecord = this.movingPosRecord[0],
x = _this$movingPosRecord.x,
y = _this$movingPosRecord.y;
var left = event.touches[0].clientX - x;
var top = event.touches[0].clientY - y;
DISTANCE_LEFT = left;
DISTANCE_TOP = top;
this.detectImgPosIsEdge();
} else {
// 以两指为坐标点 做直角三角形 a2 + b2 = c2
var width = Math.abs(event.touches[1].clientX - event.touches[0].clientX);
var height = Math.abs(event.touches[1].clientY - event.touches[0].clientY);
var hypotenuse = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2));
var scale = this.imgScale * (hypotenuse / this.fingerDistance);
this.imgScale = Math.min(scale, this.maxScale);
this.detectImgScaleIsEdge();
this.fingerDistance = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2));
}
event.preventDefault();
},
/**
* @description {图片区} 拖动结束
*/
handleImgTouchEnd: function handleImgTouchEnd() {
this.IS_TOUCH_END = true;
},
/**
* @description 旋转图片
*/
handleRotate: function handleRotate() {
this.setRoate(this.imgAngle - 90);
},
/**
* @description 取消裁剪图片
*/
handleCancel: function handleCancel() {
this.$emit('cancel');
this.$emit('input', false);
},
/**
* @description 完成裁剪
*/
handleConfirm: function handleConfirm(event) {
this.draw();
},
base64ToBlob: function base64ToBlob() {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref$source = _ref.source,
source = _ref$source === void 0 ? '' : _ref$source,
_ref$contentType = _ref.contentType,
contentType = _ref$contentType === void 0 ? 'image/png' : _ref$contentType,
_ref$sliceSize = _ref.sliceSize,
sliceSize = _ref$sliceSize === void 0 ? 512 : _ref$sliceSize;
var callback = arguments.length > 1 ? arguments[1] : undefined;
// 使用 atob() 方法将数据解码
var byteCharacters = atob(source);
var byteArrays = [];
for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
var slice = byteCharacters.slice(offset, offset + sliceSize);
var byteNumbers = [];
for (var i = 0; i < slice.length; i++) {
byteNumbers.push(slice.charCodeAt(i));
} // 8 位无符号整数值的类型化数组。内容将初始化为 0。
// 如果无法分配请求数目的字节,则将引发异常。
byteArrays.push(new Uint8Array(byteNumbers));
}
callback(new Blob(byteArrays, {
type: contentType
}));
},
blobToDataURL: function blobToDataURL(blob, callback) {
var reader = new FileReader();
reader.onload = function (e) {
callback(e.target.result);
};
reader.readAsDataURL(blob);
},
/**
* @description canvas绘制,用canvas模拟裁剪框 对根据图片当前的裁剪信息进行模拟
*/
draw: function draw() {
var _this5 = this;
if (!this.imgSrc) return;
var imgSrc = this.imgSrc,
picWidth = this.picWidth,
picHeight = this.picHeight,
imgLeft = this.imgLeft,
imgTop = this.imgTop,
imgScale = this.imgScale,
imgAngle = this.imgAngle,
cutLeft = this.cutLeft,
cutTop = this.cutTop,
cutHeight = this.cutHeight,
cutWidth = this.cutWidth,
exportScale = this.exportScale,
disabledRotate = this.disabledRotate;
var draw = function draw() {
// 图片真实大小
var width = picWidth * imgScale * exportScale;
var height = picHeight * imgScale * exportScale; // 取裁剪框和图片的交集
var x = imgLeft - cutLeft;
var y = imgTop - cutTop;
var image = document.createElement('img');
image.src = imgSrc;
image.onload = function () {
_this5.ctx.translate(x * exportScale, y * exportScale); // 设置 旋转角度
if (!disabledRotate) {
_this5.ctx.rotate(imgAngle * Math.PI / 180);
}
_this5.ctx.drawImage(image, -width / 2, -height / 2, width, height); // 生成图片
var src = _this5.canvas.toDataURL("image/".concat(_this5.fileType), _this5.quality);
src = src.split(',')[1];
_this5.base64ToBlob({
source: src,
contentType: "image/".concat(_this5.fileType)
}, function (blob) {
_this5.blobToDataURL(blob, function (dataUrl) {
_this5.$emit('confirm', {
url: dataUrl,
file: new File([blob], "".concat(_this5.filename, ".").concat(_this5.fileType), {
type: "image/".concat(_this5.fileType)
}),
width: cutWidth * exportScale,
height: cutHeight * exportScale
});
_this5.$emit('input', false);
});
});
};
};
if (this.ctx.width !== cutWidth || this.ctx.height !== cutHeight) {
this.canvasHeight = cutHeight;
this.canvasWidth = cutWidth;
draw();
} else {
draw();
}
},
onPopstate: function onPopstate() {
this.show && this.handleCancel();
}
},
created: function created() {
window.addEventListener('popstate', this.onPopstate);
},
beforeDestroy: function beforeDestroy() {
// 移除监听
window.removeEventListener('popstate', this.onPopstate);
}
});
// CONCATENATED MODULE: ./packages/img-cropper/src/main.vue?vue&type=script&lang=js&
/* harmony default export */ var src_mainvue_type_script_lang_js_ = (mainvue_type_script_lang_js_);
// EXTERNAL MODULE: ./node_modules/vue-loader/lib/runtime/componentNormalizer.js
var componentNormalizer = __webpack_require__(0);
// CONCATENATED MODULE: ./packages/img-cropper/src/main.vue
/* normalize component */
var component = Object(componentNormalizer["a" /* default */])(
src_mainvue_type_script_lang_js_,
render,
staticRenderFns,
false,
null,
null,
null
)
/* hot reload */
if (false) { var api; }
component.options.__file = "packages/img-cropper/src/main.vue"
/* harmony default export */ var main = (component.exports);
// CONCATENATED MODULE: ./packages/img-cropper/index.js
main.install = function (Vue) {
Vue.component(main.name, main);
};
/* harmony default export */ var img_cropper = __webpack_exports__["default"] = (main);
/***/ })
/******/ });