1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Vue.js で ColorPicker (iro.js) を使う方法

Last updated at Posted at 2018-11-01

タイトル通り。

iro.js という Color Picker をどうしても使いたかった、Vue.jsで。1
しかし、Vue.js向けには作られていないのでちょっとだけ使い方を工夫する必要があった。
具体的な方法についてはGithub上でおこなわれた議論(?)の途中に出てきた template の書き方を参考にした。2

実行結果

以下画像。
この丸い、Color Pickerが良かった。(他のは四角いの多い)
スクリーンショット 2018-11-02 0.41.42.png

インストール

$ npm install @jaames/iro

ソースコード

ColorPicker.vue
<template>
  <div :id="'iro-'+id"></div>
</template>

<script>
import iro from '@jaames/iro'

let colorPicker;
export default {
  name: 'color-picker',
  props: ['value', 'config'],
  data() {
    return {
      id: this._uid,
    }
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      if (!this.value || this.value.length > 7) {
        this.config.color = '#ffffff'
      } else if (this.value.length === 6) {
        this.config.color = '#' + this.value
      } else {
        this.config.color = this.value;
      }

      colorPicker = new iro.ColorPicker('#iro-' + this.id, this.config);
      colorPicker.on('color:change', this.emitColor)
    },
    updateConfig() {
      // TODO - watch for config changes and apply them
    },
    emitColor(color) {
      this.$emit('input', color.hexString);
    }
  },
  watch: {
    value: (to) => {
      if (colorPicker && colorPicker.color && to.length <= 7) {
        colorPicker.color.hexString = to;
      }
    },
    config: () => {
      this.updateConfig()
    }
  }
}
</script>

<style scoped>

</style>
  1. デザイン的に、丸いのが可愛いからです。

  2. https://github.com/jaames/iro.js/issues/30 の議論にでているソースコードを参考にした。参考先は adrianjost/SmartLight-Firebase である。またライセンスが不明であるがため、参考にした点で何らかの問題があるかもしれないが、 MIT LICENSE である iro.js の Issue 上に特に料金体系等に関する記載なく掲載しているためMITと仮定して使用した。元のソースコードを監視(watch)し、ライセンスが適用されればそのライセンスに従う。また、ライセンスが無いことを指摘し作って欲しいというIssueを作成した。これ

1
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?