1.0.42 • Published 3 years ago

vue-plain-avatar-uploader v1.0.42

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Vue Plain Avatar Uploader

This is a plain vue component used to help cropping image when choosing one image as avatar.

Getting Started

Firstly, install this component:

npm install vue-plain-avatar-uploader

Secondly, import and use this component:

<template>
  <avatar-uploader />
</template>
<script>
import AvatarUploader from 'vue-plain-avatar-uploader';

export default {
  name: 'App',
  components: {
    AvatarUploader
  }
};
</script>

More detailed example is in this REPO.

Component API

Props

NameTypeDefaultDescription
heightnumber150height of this component
maskPropsobjectundefinedcustomize mask above image, details are below
outputPropsobjectundefinedcustomize mask above image, details are below
backgroundColorstring'#f2f3f8'background color of container containing image
maxMultiplenumber2maximum multiple of enlarging image, the reference is container
hideShapeGroupbooleanundefinedsometimes, we don't need to change shape of preview area
imgstringundefinedurl that can be accepted by attribute 'src' of element 'img', to set an image in advance

Slots

action.innerSelectButton

When no image is selected, there should be a button used to open file manager in preview area of mask layer.

This slot only provides a function click, an usage example in framework vuetify is below:

<template>
  <avatar-uploader>
    <template #[`action.innerSelectButton`]="{ on }">
      <v-icon v-on="on">mdi-upload</v-icon>
    </template>
  </avatar-uploader>
</template>

action.slider

So called slider is used to adjust image size, and also an example is below:

<template>
  <avatar-uploader>
    <template #[`action.slider`]="{ on, attrs }">
      <v-slider v-on="on" v-bind="attrs" step="0.01" hide-details />
    </template>
  </avatar-uploader>
</template>

action.shapeGroup

Used to change shape of preview area at mask layer, an example is below:

<template>
  <avatar-uploader>
    <template #[`action.shapeGroup`]="{ on, attrs, options }">
      <v-radio-group @change="on.input" v-bind="attrs" row dense hide-details class="mt-0">
        <v-radio :label="option.text" :value="option.value" v-for="(option, index) in options" :key="index"></v-radio>
      </v-radio-group>
    </template>
  </avatar-uploader>
</template>

* Instead of function 'input', component radio-group of vuetify use function 'change' to implement bidirectional binding

action.selectButton

Similar with action.selectInnerButton functionally, but the position of this slot is at right side of component. Specially, this slot provides an attribute text, which indicates whether an image has been selected or not.

<template>
  <avatar-uploader>
    <template #[`action.selectButton`]="{ on, text }">
      <v-btn v-on="on" small>{{ text }}</v-btn>
    </template>
  </avatar-uploader>
</template>

* Recklessly, value of attribute 'text' is one of '选择' and '重选'

action.confirmButton

Finally, we finish adjusting and cropping image, and click one confirming button. This slot provides a function confirm, which we will emit function 'confirm' injected by parent component after triggering.

<template>
  <avatar-uploader>
    <template #[`action.confirmButton`]="{ on, attrs }">
      <v-btn v-on="on" v-bind="attrs" small>确定</v-btn>
    </template>
  </avatar-uploader>
</template>

Events

NameDescription
confirmexport output to parent component as an url (created by function canvas.toDataUrl)

MaskProps

{
    // color of mask layer, the default is '#00000025'
    // eg. '#00000025', 'rgba(255,255,255, 0.37)'
    shadowColor: string,
    // border-radius of transparent area of mask
    // the default is '5px'
    // only works when shape of transparent area is set as 'square'
    squareBorderRadius: string,
}

OutputProps

{
    // size of output image
    // the default is [100, 100]
    outputSize: array<number>,
    // format of output image
    // the default is 'png'
    // eg. 'png', 'jpeg'
    format: string,
    // when selected part of image can not fill a square
    // we will see the background
    // the default is 'transparent' (when format is 'png') and '#fff' (other format)
    // nb. 'transparent' only works when 'png' format
    backgroundColor: string,
}

Usage of slots in vuetify

For better understanding of those slots, here's one example in vuetify:

<template>
  <avatar-uploader @confirm="confirmAvatar" hideShapeGroup :img="avatar">
    <template #[`action.innerSelectButton`]="{ on }">
      <v-icon v-on="on">mdi-upload</v-icon>
    </template>
    <template #[`action.slider`]="{ on, attrs }">
      <v-slider v-on="on" v-bind="attrs" step="0.01" hide-details />
    </template>
    <template #[`action.shapeGroup`]="{ on, attrs, options }">
      <v-radio-group @change="on.input" v-bind="attrs" row dense hide-details class="mt-0">
        <v-radio :label="option.text" :value="option.value" v-for="(option, index) in options" :key="index"></v-radio>
      </v-radio-group>
    </template>
    <template #[`action.selectButton`]="{ on, text }">
      <v-btn v-on="on" small>{{ text }}</v-btn>
    </template>
    <template #[`action.confirmButton`]="{ on, attrs }">
      <v-btn v-on="on" v-bind="attrs" small>确定</v-btn>
    </template>
  </avatar-uploader>
</template>
<script>
export default {
  components: {
    AvatarUploader: () => import('vue-plain-avatar-uploader')
  }
  data() {
    return {
      avatar: 'xxx.png'
    };
  },
};
</script>

Result is showing up in the second GIF of REPO README.

Morover

CN version is here.

1.0.42

3 years ago

1.0.4

3 years ago

1.0.41

3 years ago

1.0.2

3 years ago

1.0.28

3 years ago

1.0.27

3 years ago

1.0.3

3 years ago

1.0.22

3 years ago

1.0.21

3 years ago

1.0.26

3 years ago

1.0.25

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago