canvas-color-disc 1.0.13 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -14,52 +14,61 @@
14
14
  ![avatar](https://udfs.unisiot.com/group1/M00/03/85/rBEBA2FqlXOAfw1hAAECEWBl9Kc837.png)
15
15
 
16
16
  <img src="https://udfs.unisiot.com/group1/M00/03/85/rBEBA2FqlXOAfw1hAAECEWBl9Kc837.png" width="50%">
17
+
17
18
  ## Build Setup
18
19
 
19
20
  ``` bash
20
21
  # install dependencies
21
- npm install
22
+ npm i canvas-color-disc --save
22
23
  ```
23
-
24
+ yarn
24
25
  ``` bash
25
- # serve examples at localhost:8080
26
- npm run start
26
+ # install dependencies
27
+ yarn add canvas-color-disc
28
+ ```
29
+ main.js添加组件
30
+ ```
31
+ import CanvasColorDisc from 'canvas-color-disc'
32
+ Vue.use(CanvasColorDisc)
33
+
27
34
  ```
28
35
 
29
36
  ```vue
30
37
  <template>
31
38
  <div id="app">
32
39
  <p>RGB: {{RGB}}</p>
40
+ <p>Hex: {{Hex}}</p>
33
41
  <p>changing RGB: {{changingRGB}}</p>
34
- <canvas-color-disc
35
- vals="0"
36
- :rgb="RGB"
42
+ <p>changing Hex: {{changingHex}}</p>
43
+ <CanvasColorDisc
44
+ id="0"
45
+ :color="RGB"
37
46
  :width="300"
38
47
  :height="240"
39
48
  @changing="changing"
40
- @change="change"></canvas-color-disc>
49
+ @change="changeColor"></CanvasColorDisc>
41
50
  </div>
42
51
  </template>
43
52
 
44
53
  <script>
45
- import CanvasColorDisc from './components/CanvasColorDisc.vue'
46
54
  export default {
47
55
  name: 'App',
48
- components: {
49
- CanvasColorDisc,
50
- },
51
56
  data() {
52
57
  return {
53
58
  RGB: { r: 255, g:255, b:255 },
54
59
  changingRGB: '',
60
+ changingHex: '',
61
+ Hex: '',
55
62
  }
56
63
  },
57
64
  methods: {
58
- change(newRgb, utils) {
65
+ changeColor(newRgb, utils) {
59
66
  this.RGB = newRgb
67
+ this.Hex = `#${utils.RGBToHex(`(${newRgb.r}, ${newRgb.g}, ${newRgb.b})`)}`.toUpperCase()
60
68
  },
61
69
  changing(newRgb, utils) {
62
70
  this.changingRGB = JSON.stringify(newRgb)
71
+ this.changingHex = `#${utils.RGBToHex(`(${newRgb.r}, ${newRgb.g}, ${newRgb.b})`)}`.toUpperCase()
63
72
  },
64
73
  },
65
74
  }
@@ -72,3 +81,16 @@ export default {
72
81
  </style>
73
82
  ```
74
83
 
84
+ | 参数 | 说明 | 类型 | 默认值 |
85
+ | ------------- | ----------- | ----------------------------------------------- | ------ |
86
+ | id | canvas id拼接 | String | "0" |
87
+ | color | 默认颜色 | Object | {"r": 255, "g": 255, "b": 255} |
88
+ | width | canvas画板宽度 | Number | window.innerWidth * 0.7 |
89
+ | height | canvas画板高度 | Number | window.innerWidth * 0.78 |
90
+
91
+
92
+
93
+ | 事件 | 说明 | 回调参数 |
94
+ | ------------- | ----------- | ----------------------------------------------- |
95
+ | changing | 滑动选择回调 | rgb: 颜色, Util: 内置util,可以转换颜色(Util.hvsToRGB、Util.RGBToHvs、Util.RGBToHex、Util.hexToRGB、Util.RGBToHsv、Util.hsvToRgb) |
96
+ | change | 选择结束回调 | rgb: 颜色, Util: 内置util,可以转换颜色(Util.hvsToRGB、Util.RGBToHvs、Util.RGBToHex、Util.hexToRGB、Util.RGBToHsv、Util.hsvToRgb) |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "canvas-color-disc",
3
- "version": "1.0.13",
3
+ "version": "1.1.0",
4
4
  "description": "Color disc realized by canvas",
5
5
  "private": false,
6
6
  "main": "src/index.js",
@@ -40,15 +40,11 @@
40
40
  "babel-jest": "^21.0.2",
41
41
  "babel-loader": "^7.1.1",
42
42
  "babel-plugin-dynamic-import-node": "^1.2.0",
43
- "babel-plugin-syntax-jsx": "^6.18.0",
44
43
  "babel-plugin-transform-es2015-modules-commonjs": "^6.26.0",
45
44
  "babel-plugin-transform-runtime": "^6.22.0",
46
- "babel-plugin-transform-vue-jsx": "^3.5.0",
47
45
  "babel-preset-env": "^1.3.2",
48
46
  "babel-preset-stage-2": "^6.22.0",
49
47
  "babel-register": "^6.22.0",
50
- "chalk": "^2.0.1",
51
- "chromedriver": "^2.27.2",
52
48
  "copy-webpack-plugin": "^4.0.1",
53
49
  "cross-spawn": "^5.0.1",
54
50
  "css-loader": "^0.28.0",
@@ -62,25 +58,19 @@
62
58
  "eslint-plugin-standard": "^3.0.1",
63
59
  "eslint-plugin-vue": "^5.2.3",
64
60
  "extract-text-webpack-plugin": "^3.0.0",
65
- "file-loader": "^1.1.4",
66
61
  "friendly-errors-webpack-plugin": "^1.6.1",
67
62
  "glob-all": "^3.1.0",
68
63
  "html-webpack-plugin": "^2.30.1",
69
64
  "jest": "^22.0.4",
70
65
  "jest-serializer-vue": "^0.3.0",
71
- "mockjs": "^1.0.1-beta3",
72
- "nightwatch": "^0.9.12",
73
66
  "node-notifier": "^5.1.2",
74
67
  "node-sass": "^4.12.0",
75
68
  "optimize-css-assets-webpack-plugin": "^3.2.0",
76
- "ora": "^1.2.0",
77
- "portfinder": "^1.0.13",
78
69
  "postcss-import": "^11.0.0",
79
70
  "postcss-loader": "^2.0.8",
80
71
  "postcss-url": "^7.2.1",
81
72
  "purify-css": "^1.2.5",
82
73
  "purifycss-webpack": "^0.7.0",
83
- "quill": "^1.3.6",
84
74
  "rimraf": "^2.6.0",
85
75
  "sass-loader": "^7.3.1",
86
76
  "selenium-server": "^3.0.1",
package/src/App.vue CHANGED
@@ -9,10 +9,12 @@
9
9
  <template>
10
10
  <div id="app">
11
11
  <p>RGB: {{RGB}}</p>
12
+ <p>Hex: {{Hex}}</p>
12
13
  <p>changing RGB: {{changingRGB}}</p>
14
+ <p>changing Hex: {{changingHex}}</p>
13
15
  <CanvasColorDisc
14
- vals="0"
15
- :rgb="RGB"
16
+ id="0"
17
+ :color="RGB"
16
18
  :width="300"
17
19
  :height="240"
18
20
  @changing="changing"
@@ -27,14 +29,18 @@ export default {
27
29
  return {
28
30
  RGB: { r: 255, g:255, b:255 },
29
31
  changingRGB: '',
32
+ changingHex: '',
33
+ Hex: '',
30
34
  }
31
35
  },
32
36
  methods: {
33
37
  changeColor(newRgb, utils) {
34
38
  this.RGB = newRgb
39
+ this.Hex = `#${utils.RGBToHex(`(${newRgb.r}, ${newRgb.g}, ${newRgb.b})`)}`.toUpperCase()
35
40
  },
36
41
  changing(newRgb, utils) {
37
42
  this.changingRGB = JSON.stringify(newRgb)
43
+ this.changingHex = `#${utils.RGBToHex(`(${newRgb.r}, ${newRgb.g}, ${newRgb.b})`)}`.toUpperCase()
38
44
  },
39
45
  },
40
46
  }
@@ -1,13 +1,13 @@
1
1
  <template>
2
2
  <div class="rgb-color" style="width:100%;">
3
- <div class="colorPalatte" :class="`colorPalatte${vals}`">
3
+ <div class="colorPalatte" :class="`colorPalatte${id}`">
4
4
  <canvas
5
5
  class="cans"
6
6
  :width="width"
7
- :id="`canvasBg${vals}`"
7
+ :id="`canvasBg${id}`"
8
8
  :height="height" />
9
9
  <canvas
10
- class="cans ids" :width="width" :id="`canvasSelector${vals}`" :height="height" @touchmove="handle"
10
+ class="cans ids" :width="width" :id="`canvasSelector${id}`" :height="height" @touchmove="handle"
11
11
  @touchend="endhandle" @mousedown="mouseDown" @mousemove="mouseMove" @mouseup="mouseUp" />
12
12
  </div>
13
13
  </div>
@@ -20,29 +20,33 @@ import Util from './../uitl.js';
20
20
  export default {
21
21
  name: 'CanvasColorDisc',
22
22
  props: {
23
- vals: {
23
+ // canvas id
24
+ id: {
24
25
  type: String,
25
26
  default: '0',
26
27
  required: true,
27
28
  },
28
- rgb: {
29
+ // 颜色值,可以是rgb的JSON格式,也可以是String
30
+ color: {
29
31
  type: Object,
30
32
  default: () => {
31
33
  return { r: 255, g: 255, b: 255 };
32
34
  },
33
35
  required: true,
34
36
  },
37
+ // 画布宽度
35
38
  width: {
36
39
  type: Number,
37
40
  default: window.innerWidth * 0.7, // 画布宽
38
41
  },
42
+ // 画布高度
39
43
  height: {
40
44
  type: Number,
41
45
  default: window.innerWidth * 0.78, // 画布高
42
46
  },
43
47
  },
44
48
  watch: {
45
- rgb: {
49
+ color: {
46
50
  handler: function (val) {
47
51
  this.r = val.r;
48
52
  this.g = val.g;
@@ -77,16 +81,16 @@ export default {
77
81
  };
78
82
  },
79
83
  mounted() {
80
- const cpt = window.document.querySelector(`#canvasSelector${this.vals}`);
84
+ const cpt = window.document.querySelector(`#canvasSelector${this.id}`);
81
85
  this.centerX = this.width / 2;
82
86
  this.centerY = this.height / 2;
83
87
  // this.leftColor = (document.body.clientWidth-this.width)/2;
84
- this.canvasBg = window.document.querySelector(`#canvasBg${this.vals}`).getContext('2d');
85
- this.cstor = window.document.querySelector(`#canvasSelector${this.vals}`);
86
- this.canvasSelector = window.document.querySelector(`#canvasSelector${this.vals}`).getContext('2d');
88
+ this.canvasBg = window.document.querySelector(`#canvasBg${this.id}`).getContext('2d');
89
+ this.cstor = window.document.querySelector(`#canvasSelector${this.id}`);
90
+ this.canvasSelector = window.document.querySelector(`#canvasSelector${this.id}`).getContext('2d');
87
91
 
88
92
  //解决锯齿
89
- var canvas = window.document.querySelector(`#canvasBg${this.vals}`);
93
+ var canvas = window.document.querySelector(`#canvasBg${this.id}`);
90
94
  var width = canvas.width;
91
95
  var height = canvas.height;
92
96
  if (window.devicePixelRatio) {
@@ -169,11 +173,22 @@ export default {
169
173
  },
170
174
  mouseMove(e) {
171
175
  if (this.mouseStart) {
172
- this.endClick(e)
176
+ e.preventDefault();
177
+ const xl = e.clientX;
178
+ const yl = e.clientY;
179
+ let nx = xl - this.cstor.getBoundingClientRect().left;
180
+ let ny = yl - this.cstor.getBoundingClientRect().top;
181
+ this.getRgbbyxy(nx, ny);
173
182
  }
174
183
  },
175
184
  mouseUp(e) {
176
185
  this.mouseStart = false;
186
+ e.preventDefault();
187
+ const xl = e.clientX;
188
+ const yl = e.clientY;
189
+ let nx = xl - this.cstor.getBoundingClientRect().left;
190
+ let ny = yl - this.cstor.getBoundingClientRect().top;
191
+ this.getRgbbyxyend(nx, ny);
177
192
  },
178
193
  endClick(e) {
179
194
  e.preventDefault();
@@ -186,7 +201,7 @@ export default {
186
201
  getRgbbyxyend(xt, yt) {
187
202
  const rad = Util.getSelectorRadians(xt, yt, this.centerX, this.centerY, this.radius);
188
203
  if (rad.r > this.radius) return;
189
- const rgbs = Util.hvStoRGB(rad.radian, 255, rad.r);
204
+ const rgbs = Util.hvsToRGB(rad.radian, 255, rad.r);
190
205
  if (rad.r < 8) {
191
206
  rgbs.r = 255; rgbs.g = 255; rgbs.b = 255;
192
207
  }
@@ -209,7 +224,7 @@ export default {
209
224
  getRgbbyxy(xt, yt) {
210
225
  const rad = Util.getSelectorRadians(xt, yt, this.centerX, this.centerY, this.radius);
211
226
  if (rad.r > this.radius) return;
212
- const rgbs = Util.hvStoRGB(rad.radian, 255, rad.r);
227
+ const rgbs = Util.hvsToRGB(rad.radian, 255, rad.r);
213
228
  if (rad.r < 8) {
214
229
  rgbs.r = 255; rgbs.g = 255; rgbs.b = 255;
215
230
  }
package/src/uitl.js CHANGED
@@ -4,7 +4,7 @@ class Util {
4
4
  static drawColorDisk (ctx, x, y, r) {
5
5
  for (let i = 0; i <= r; i+=0.4) {
6
6
  for (let j = 0; j < Math.PI*2; j+= Math.PI/90) {
7
- const rgbarr = this.hvStoRGB(j,255,i);
7
+ const rgbarr = this.hvsToRGB(j,255,i);
8
8
  ctx.strokeStyle = 'rgb('+rgbarr.r+','+rgbarr.g+','+rgbarr.b+')';
9
9
  ctx.beginPath();
10
10
  ctx.arc(x,y,i,-j,-j-Math.PI/90,true);
@@ -13,7 +13,7 @@ class Util {
13
13
  }
14
14
  return false;
15
15
  }
16
- static hvStoRGB(hi, vi, si) {
16
+ static hvsToRGB(hi, vi, si) {
17
17
  let h = hi * 180 / Math.PI;
18
18
  const rgb = {};
19
19
  h = Math.round(h);
@@ -40,7 +40,7 @@ class Util {
40
40
  b: rgb.b < 0 ? 0 : Math.round(rgb.b)
41
41
  };
42
42
  }
43
- static RGBtoHvs(r, g, b) {
43
+ static RGBToHvs(r, g, b) {
44
44
  const rgb = {
45
45
  r: r,
46
46
  g: g,
@@ -71,7 +71,7 @@ class Util {
71
71
  return hsb;
72
72
  }
73
73
  static selectorLocation (rgb) {
74
- const hsb = this.RGBtoHvs(rgb.r, rgb.g, rgb.b);
74
+ const hsb = this.RGBToHvs(rgb.r, rgb.g, rgb.b);
75
75
  let r = hsb.s;
76
76
  let h =360 - hsb.h;
77
77
  let y = Math.sin(h * Math.PI/180)*r;
@@ -92,7 +92,7 @@ class Util {
92
92
  return { x: x, y: y };
93
93
  }
94
94
  static rMax (rgb) {
95
- const hsb = this.RGBtoHvs(rgb.r, rgb.g, rgb.b);
95
+ const hsb = this.RGBToHvs(rgb.r, rgb.g, rgb.b);
96
96
  let r = hsb.s;
97
97
  let h =360-hsb.h;
98
98
  let y = Math.sin(h * Math.PI/180)*r;
@@ -153,7 +153,7 @@ class Util {
153
153
  }
154
154
 
155
155
  // rgb转16进制
156
- static rgbToHex (color){
156
+ static RGBToHex (color){
157
157
  if(color.indexOf("#") != -1) {
158
158
  return color;
159
159
  }
@@ -166,7 +166,7 @@ class Util {
166
166
  return value.slice(1);
167
167
  }
168
168
 
169
- static hexTorgb (hex){
169
+ static hexToRGB (hex){
170
170
  var hexNum = hex;
171
171
  var rgb = {};
172
172
  hexNum = '0x' + (hexNum.length < 6 ? repeatLetter(hexNum, 2) : hexNum);
@@ -190,7 +190,7 @@ class Util {
190
190
  return result;
191
191
  }
192
192
  }
193
- static rgbToHsv (arr){
193
+ static RGBToHsv (arr){
194
194
  var h = 0, s = 0, v = 0;
195
195
  var r = arr[0], g = arr[1], b = arr[2];
196
196
  arr.sort(function (a, b) {