color-js--使用/实例

原文网址:color-js--使用/实例IT利刃出鞘的博客-CSDN博客

简介

说明

        支持RGB, HSV 和 HSL(他们也都支持alpha)。支持CSS字符串表达式的转换。

        这个项目作者已经不再维护了,最新的更新时间是:2018年10月26日。

        此库比较小众,从npm下载量来看,每周5000左右。而color.js​​​​​​​是12,000,000左右。

官网

npm仓库网址
github网址

实例

需求:输入颜色后,渐变展示。

安装依赖

npm i color-js

router/index.js

import Vue from 'vue'
import Router from 'vue-router'
import CompA from "@/components/CompA";

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/compA',
      name: 'compA',
      component: CompA
    }
  ]
})

components/CompA.vue

<template>
  <div class="compA">
    <input v-model="colorQuery" @keyup.enter="updateColor" placeholder="Enter a color">
    <button @click="updateColor">Update</button>
    <p>Preview:</p>
    <span :style="{ backgroundColor: tweenedCSSColor }" style="display: inline-block;width: 50px;height: 50px;"></span>
    <p>{{ tweenedCSSColor }}</p>
  </div>
</template>

<script>
import TWEEN from "@tweenjs/tween.js";
import Color from "color-js"

export default {
  data() {
    return {
      colorQuery: '',
      color: {
        red: 0,
        green: 0,
        blue: 0,
        alpha: 1
      },
      tweenedColor: {}
    }
  },

  created: function () {
    this.tweenedColor = Object.assign({}, this.color)
  },
  watch: {
    color: function () {
      function animate () {
        if (TWEEN.update()) {
          requestAnimationFrame(animate)
        }
      }
      new TWEEN.Tween(this.tweenedColor)
        .to(this.color, 750)
        .start()
      animate()
    }
  },
  computed: {
    tweenedCSSColor: function () {
      return new Color({
        red: this.tweenedColor.red,
        green: this.tweenedColor.green,
        blue: this.tweenedColor.blue,
        alpha: this.tweenedColor.alpha
      }).toCSS()
    }
  },
  methods: {
    updateColor: function () {
      this.color = new Color(this.colorQuery).toRGB()
      this.colorQuery = ''
    }
  }
}
</script>

<style scoped>
</style>

测试

访问:http://localhost:8080/#/compA

其他网址

状态过渡 - 状态动画与侦听器 - 《Vue.js v2.x 官方教程》 - 书栈网 · BookStack

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT利刃出鞘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值