Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Touch support added #48

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@caohenghu/vue-colorpicker",
"version": "1.2.4",
"version": "1.2.6",
"description": "Colorpicker of Vue Components",
"main": "src/index.js",
"scripts": {
Expand Down
89 changes: 52 additions & 37 deletions src/color/Alpha.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@
<div
class="color-alpha"
@mousedown.prevent.stop="selectAlpha"
@touchstart.prevent.stop="selectAlphaTouch"
>
<canvas ref="canvasAlpha" />
<div
:style="slideAlphaStyle"
class="slide"
/>
<Slide :style="slideAlphaStyle" />
</div>
</template>

<script>
import mixin from './mixin'
import Slide from './Slide.vue'

export default {
components: {
Slide
},
mixins: [mixin],
props: {
color: {
Expand Down Expand Up @@ -43,7 +46,7 @@ export default {
color() {
this.renderColor()
},
'rgba.a'() {
'rgba'() {
this.renderSlide()
}
},
Expand All @@ -68,37 +71,59 @@ export default {

this.createLinearGradient('p', ctx, width, height, 'rgba(255,255,255,0)', this.color)
},
changeStart () {
const { top } = this.$el.getBoundingClientRect()
this.hueTop = top
},
change (e) {
const clientY = e.clientY || e.touches[0].clientY
let y = clientY - this.hueTop

if (y < 0) {
y = 0
}
if (y > this.height) {
y = this.height
}

let a = parseFloat((y / this.height).toFixed(2))
this.$emit('selectAlpha', a)
},
changeEnd () {
delete this.hueTop
},
renderSlide() {
const { r, g, b, a } = this.rgba
this.slideAlphaStyle = {
top: this.rgba.a * this.height - 2 + 'px'
top: a * this.height - 2 + 'px',
background: `rgba(${r}, ${g}, ${b}, ${a})`
}
},
selectAlpha(e) {
const { top: hueTop } = this.$el.getBoundingClientRect()

const mousemove = e => {
let y = e.clientY - hueTop

if (y < 0) {
y = 0
}
if (y > this.height) {
y = this.height
}

let a = parseFloat((y / this.height).toFixed(2))
this.$emit('selectAlpha', a)
}

mousemove(e)
this.changeStart()
this.change(e)

const mouseup = () => {
document.removeEventListener('mousemove', mousemove)
this.changeEnd()
document.removeEventListener('mousemove', this.change)
document.removeEventListener('mouseup', mouseup)
}

document.addEventListener('mousemove', mousemove)
document.addEventListener('mousemove', this.change)
document.addEventListener('mouseup', mouseup)
},
selectAlphaTouch(e) {
this.changeStart(e)
this.change(e)

const touchend = () => {
this.changeEnd()
document.removeEventListener('touchmove', this.change)
document.removeEventListener('touchend', touchend)
}

document.addEventListener('touchmove', this.change)
document.addEventListener('touchend', touchend)
}
}
}
Expand All @@ -107,17 +132,7 @@ export default {
<style lang="scss">
.color-alpha {
position: relative;
margin-left: 8px;
margin-left: 12px;
cursor: pointer;
.slide {
position: absolute;
left: 0;
top: 100px;
width: 100%;
height: 4px;
background: #fff;
box-shadow: 0 0 1px 0 rgba(0, 0, 0, 0.3);
pointer-events: none;
}
}
</style>
</style>
6 changes: 5 additions & 1 deletion src/color/Colors.vue
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ export default {
.colors {
padding: 0;
margin: 0;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
&.history {
margin-top: 10px;
border-top: 1px solid #2e333a;
Expand Down Expand Up @@ -133,4 +137,4 @@ export default {
}
}
}
</style>
</style>
103 changes: 63 additions & 40 deletions src/color/Hue.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@
<div
class="hue"
@mousedown.prevent.stop="selectHue"
@touchstart.prevent.stop="selectHueTouch"
>
<canvas ref="canvasHue" />
<div
:style="slideHueStyle"
class="slide"
/>
<Slide :style="slideHueStyle" />
</div>
</template>

<script>
import Slide from './Slide.vue'
export default {
components: {
Slide
},
props: {
hsv: {
type: Object,
Expand All @@ -25,7 +27,11 @@ export default {
height: {
type: Number,
default: 152
}
},
color: {
type: String,
default: '#000000'
},
},
data() {
return {
Expand Down Expand Up @@ -64,60 +70,77 @@ export default {
},
renderSlide() {
this.slideHueStyle = {
top: (1 - this.hsv.h / 360) * this.height - 2 + 'px'
top: (1 - this.hsv.h / 360) * this.height - 2 + 'px',
background: this.color
}
},
selectHue(e) {
const { top: hueTop } = this.$el.getBoundingClientRect()
const ctx = e.target.getContext('2d')
changeStart (e) {
const { top } = this.$el.getBoundingClientRect()
this.hueTop = top
this.ctx = e.target.getContext('2d')
},
change (e) {
if (!this.hueTop || !this.ctx) return

const mousemove = e => {
let y = e.clientY - hueTop
const clientY = e.clientY || e.touches[0].clientY
let y = clientY - this.hueTop

if (y < 0) {
y = 0
}
if (y > this.height) {
y = this.height
}
if (y < 0) {
y = 0
}
if (y > this.height) {
y = this.height
}

this.slideHueStyle = {
top: y - 2 + 'px'
}
// 如果用最大值,选择的像素会是空的,空的默认是黑色
const imgData = ctx.getImageData(0, Math.min(y, this.height - 1), 1, 1)
const [r, g, b] = imgData.data
this.$emit('selectHue', { r, g, b })
this.slideHueStyle = {
top: y - 2 + 'px',
background: this.color
}

mousemove(e)
// 如果用最大值,选择的像素会是空的,空的默认是黑色
const imgData = this.ctx.getImageData(0, Math.min(y, this.height - 1), 1, 1)
const [r, g, b] = imgData.data

this.$emit('selectHue', { r, g, b })
},
changeEnd () {
delete this.hueTop
delete this.ctx
},
selectHue(e) {
this.changeStart(e)
this.change(e)

const mouseup = () => {
document.removeEventListener('mousemove', mousemove)
this.changeEnd()
document.removeEventListener('mousemove', this.change)
document.removeEventListener('mouseup', mouseup)
}

document.addEventListener('mousemove', mousemove)
document.addEventListener('mousemove', this.change)
document.addEventListener('mouseup', mouseup)
},
selectHueTouch(e) {
this.changeStart(e)
this.change(e)

const touchend = () => {
this.changeEnd()
document.removeEventListener('touchmove', this.change)
document.removeEventListener('touchend', touchend)
}

document.addEventListener('touchmove', this.change)
document.addEventListener('touchend', touchend)
}
}
}
</script>

<style lang="scss">
.hue {
.hue {
position: relative;
margin-left: 8px;
margin-left: 12px;
cursor: pointer;
.slide {
position: absolute;
left: 0;
top: 100px;
width: 100%;
height: 4px;
background: #fff;
box-shadow: 0 0 1px 0 rgba(0, 0, 0, 0.3);
pointer-events: none;
}
}
}
</style>
5 changes: 3 additions & 2 deletions src/color/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<Hue
ref="hue"
:hsv="hsv"
:color="modelHex"
:width="hueWidth"
:height="hueHeight"
@selectHue="selectHue"
Expand Down Expand Up @@ -138,7 +139,7 @@ export default {
return this.theme === 'light'
},
totalWidth() {
return this.hueHeight + (this.hueWidth + 8) * 2
return this.hueHeight + (this.hueWidth + 12) * 2
},
previewWidth() {
return this.totalWidth - (this.suckerHide ? 0 : this.previewHeight)
Expand Down Expand Up @@ -294,4 +295,4 @@ export default {
display: flex;
}
}
</style>
</style>
Loading