|
@@ -1,5 +1,5 @@
|
|
<template>
|
|
<template>
|
|
- <div :class="$attrs.class" :style="getWrapperStyle">
|
|
|
|
|
|
+ <div :class="getClass" :style="getWrapperStyle">
|
|
<img
|
|
<img
|
|
v-show="isReady"
|
|
v-show="isReady"
|
|
ref="imgElRef"
|
|
ref="imgElRef"
|
|
@@ -12,16 +12,16 @@
|
|
</template>
|
|
</template>
|
|
<script lang="ts">
|
|
<script lang="ts">
|
|
import type { CSSProperties } from 'vue';
|
|
import type { CSSProperties } from 'vue';
|
|
-
|
|
|
|
import { defineComponent, onMounted, ref, unref, computed } from 'vue';
|
|
import { defineComponent, onMounted, ref, unref, computed } from 'vue';
|
|
-
|
|
|
|
import Cropper from 'cropperjs';
|
|
import Cropper from 'cropperjs';
|
|
import 'cropperjs/dist/cropper.css';
|
|
import 'cropperjs/dist/cropper.css';
|
|
|
|
+ import { useDesign } from '/@/hooks/web/useDesign';
|
|
|
|
+ import { useThrottleFn } from '@vueuse/shared';
|
|
|
|
|
|
type Options = Cropper.Options;
|
|
type Options = Cropper.Options;
|
|
|
|
|
|
const defaultOptions: Options = {
|
|
const defaultOptions: Options = {
|
|
- aspectRatio: 16 / 9,
|
|
|
|
|
|
+ aspectRatio: 1,
|
|
zoomable: true,
|
|
zoomable: true,
|
|
zoomOnTouch: true,
|
|
zoomOnTouch: true,
|
|
zoomOnWheel: true,
|
|
zoomOnWheel: true,
|
|
@@ -43,40 +43,33 @@
|
|
rotatable: true,
|
|
rotatable: true,
|
|
};
|
|
};
|
|
|
|
|
|
- export default defineComponent({
|
|
|
|
- name: 'CropperImage',
|
|
|
|
- props: {
|
|
|
|
- src: {
|
|
|
|
- type: String,
|
|
|
|
- required: true,
|
|
|
|
- },
|
|
|
|
- alt: {
|
|
|
|
- type: String,
|
|
|
|
- },
|
|
|
|
- height: {
|
|
|
|
- type: [String, Number],
|
|
|
|
- default: '360px',
|
|
|
|
- },
|
|
|
|
- crossorigin: {
|
|
|
|
- type: String as PropType<'' | 'anonymous' | 'use-credentials' | undefined>,
|
|
|
|
- default: undefined,
|
|
|
|
- },
|
|
|
|
- imageStyle: {
|
|
|
|
- type: Object as PropType<CSSProperties>,
|
|
|
|
- default: () => ({}),
|
|
|
|
- },
|
|
|
|
- options: {
|
|
|
|
- type: Object as PropType<Options>,
|
|
|
|
- default: () => ({}),
|
|
|
|
- },
|
|
|
|
|
|
+ const props = {
|
|
|
|
+ src: { type: String, required: true },
|
|
|
|
+ alt: { type: String },
|
|
|
|
+ circled: { type: Boolean, default: false },
|
|
|
|
+
|
|
|
|
+ realTimePreview: { type: Boolean, default: true },
|
|
|
|
+ height: { type: [String, Number], default: '360px' },
|
|
|
|
+ crossorigin: {
|
|
|
|
+ type: String as PropType<'' | 'anonymous' | 'use-credentials' | undefined>,
|
|
|
|
+ default: undefined,
|
|
},
|
|
},
|
|
- emits: ['cropperedInfo'],
|
|
|
|
- setup(props, ctx) {
|
|
|
|
- const imgElRef = ref<ElRef<HTMLImageElement>>(null);
|
|
|
|
- const cropper: any = ref<Nullable<Cropper>>(null);
|
|
|
|
|
|
+ imageStyle: { type: Object as PropType<CSSProperties>, default: () => ({}) },
|
|
|
|
+ options: { type: Object as PropType<Options>, default: () => ({}) },
|
|
|
|
+ };
|
|
|
|
|
|
|
|
+ export default defineComponent({
|
|
|
|
+ name: 'CropperImage',
|
|
|
|
+ props,
|
|
|
|
+ emits: ['cropend', 'ready', 'cropendError'],
|
|
|
|
+ setup(props, { attrs, emit }) {
|
|
|
|
+ const imgElRef = ref<ElRef<HTMLImageElement>>();
|
|
|
|
+ const cropper = ref<Nullable<Cropper>>();
|
|
const isReady = ref(false);
|
|
const isReady = ref(false);
|
|
|
|
|
|
|
|
+ const { prefixCls } = useDesign('cropper-image');
|
|
|
|
+ const throttleRealTimeCroppered = useThrottleFn(realTimeCroppered, 80);
|
|
|
|
+
|
|
const getImageStyle = computed((): CSSProperties => {
|
|
const getImageStyle = computed((): CSSProperties => {
|
|
return {
|
|
return {
|
|
height: props.height,
|
|
height: props.height,
|
|
@@ -85,11 +78,22 @@
|
|
};
|
|
};
|
|
});
|
|
});
|
|
|
|
|
|
|
|
+ const getClass = computed(() => {
|
|
|
|
+ return [
|
|
|
|
+ prefixCls,
|
|
|
|
+ attrs.class,
|
|
|
|
+ {
|
|
|
|
+ [`${prefixCls}--circled`]: props.circled,
|
|
|
|
+ },
|
|
|
|
+ ];
|
|
|
|
+ });
|
|
|
|
+
|
|
const getWrapperStyle = computed((): CSSProperties => {
|
|
const getWrapperStyle = computed((): CSSProperties => {
|
|
- const { height } = props;
|
|
|
|
- return { height: `${height}`.replace(/px/, '') + 'px' };
|
|
|
|
|
|
+ return { height: `${props.height}`.replace(/px/, '') + 'px' };
|
|
});
|
|
});
|
|
|
|
|
|
|
|
+ onMounted(init);
|
|
|
|
+
|
|
async function init() {
|
|
async function init() {
|
|
const imgEl = unref(imgElRef);
|
|
const imgEl = unref(imgElRef);
|
|
if (!imgEl) {
|
|
if (!imgEl) {
|
|
@@ -99,29 +103,83 @@
|
|
...defaultOptions,
|
|
...defaultOptions,
|
|
ready: () => {
|
|
ready: () => {
|
|
isReady.value = true;
|
|
isReady.value = true;
|
|
|
|
+ realTimeCroppered();
|
|
|
|
+ emit('ready', cropper.value);
|
|
|
|
+ },
|
|
|
|
+ crop() {
|
|
|
|
+ throttleRealTimeCroppered();
|
|
|
|
+ },
|
|
|
|
+ zoom() {
|
|
|
|
+ throttleRealTimeCroppered();
|
|
|
|
+ },
|
|
|
|
+ cropmove() {
|
|
|
|
+ throttleRealTimeCroppered();
|
|
},
|
|
},
|
|
...props.options,
|
|
...props.options,
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // Real-time display preview
|
|
|
|
+ function realTimeCroppered() {
|
|
|
|
+ props.realTimePreview && croppered();
|
|
|
|
+ }
|
|
|
|
+
|
|
// event: return base64 and width and height information after cropping
|
|
// event: return base64 and width and height information after cropping
|
|
- const croppered = (): void => {
|
|
|
|
|
|
+ function croppered() {
|
|
|
|
+ if (!cropper.value) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
let imgInfo = cropper.value.getData();
|
|
let imgInfo = cropper.value.getData();
|
|
- cropper.value.getCroppedCanvas().toBlob((blob) => {
|
|
|
|
|
|
+ const canvas = props.circled ? getRoundedCanvas() : cropper.value.getCroppedCanvas();
|
|
|
|
+ canvas.toBlob((blob) => {
|
|
|
|
+ if (!blob) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
let fileReader: FileReader = new FileReader();
|
|
let fileReader: FileReader = new FileReader();
|
|
|
|
+ fileReader.readAsDataURL(blob);
|
|
fileReader.onloadend = (e) => {
|
|
fileReader.onloadend = (e) => {
|
|
- ctx.emit('cropperedInfo', {
|
|
|
|
|
|
+ emit('cropend', {
|
|
imgBase64: e.target?.result ?? '',
|
|
imgBase64: e.target?.result ?? '',
|
|
imgInfo,
|
|
imgInfo,
|
|
});
|
|
});
|
|
};
|
|
};
|
|
- fileReader.readAsDataURL(blob);
|
|
|
|
- }, 'image/jpeg');
|
|
|
|
- };
|
|
|
|
|
|
+ fileReader.onerror = () => {
|
|
|
|
+ emit('cropendError');
|
|
|
|
+ };
|
|
|
|
+ }, 'image/png');
|
|
|
|
+ }
|
|
|
|
|
|
- onMounted(init);
|
|
|
|
|
|
+ // Get a circular picture canvas
|
|
|
|
+ function getRoundedCanvas() {
|
|
|
|
+ const sourceCanvas = cropper.value!.getCroppedCanvas();
|
|
|
|
+ const canvas = document.createElement('canvas');
|
|
|
|
+ const context = canvas.getContext('2d')!;
|
|
|
|
+ const width = sourceCanvas.width;
|
|
|
|
+ const height = sourceCanvas.height;
|
|
|
|
+ canvas.width = width;
|
|
|
|
+ canvas.height = height;
|
|
|
|
+ context.imageSmoothingEnabled = true;
|
|
|
|
+ context.drawImage(sourceCanvas, 0, 0, width, height);
|
|
|
|
+ context.globalCompositeOperation = 'destination-in';
|
|
|
|
+ context.beginPath();
|
|
|
|
+ context.arc(width / 2, height / 2, Math.min(width, height) / 2, 0, 2 * Math.PI, true);
|
|
|
|
+ context.fill();
|
|
|
|
+ return canvas;
|
|
|
|
+ }
|
|
|
|
|
|
- return { imgElRef, getWrapperStyle, getImageStyle, isReady, croppered };
|
|
|
|
|
|
+ return { getClass, imgElRef, getWrapperStyle, getImageStyle, isReady, croppered };
|
|
},
|
|
},
|
|
});
|
|
});
|
|
</script>
|
|
</script>
|
|
|
|
+<style lang="less">
|
|
|
|
+ @prefix-cls: ~'@{namespace}-cropper-image';
|
|
|
|
+
|
|
|
|
+ .@{prefix-cls} {
|
|
|
|
+ &--circled {
|
|
|
|
+ .cropper-view-box,
|
|
|
|
+ .cropper-face {
|
|
|
|
+ border-radius: 50%;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+</style>
|