chenjf5 / note

1 stars 0 forks source link

js #3

Open chenjf5 opened 7 years ago

chenjf5 commented 7 years ago

如何实现拖动效果

chenjf5 commented 7 years ago

在react中,按钮实现点击效果

import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import classnames from 'classnames';
import './buttonAnim.less';
/**
 * ButtonAnimate组件是给按钮添加特效的组件
 * 1 color 可选,点击时背景变化颜色
 * 2 wrapClassName 可选,覆盖改类的样式
 * @classdesc ButtonAnimate
 * @extends Component
 */
export default class ButtonAnimate extends Component { 
  constructor(props) {
    super(props);
    this.btn = null;
    this.RETCOLOE = '#fff';
  }

  static defaultProps = {
    color: 'rgba(218, 213, 213, 1)',
    wrapClassName: 'snail'
  }
  static propTypes = {
    color: React.PropTypes.string,
    wrapClassName: React.PropTypes.string
  }

  componentDidMount() {
    const containers = ReactDOM.findDOMNode(this.btn);
    this.canvas = document.createElement('canvas');
    this.canvas.addEventListener('click', this.press, false);
    containers.appendChild(this.canvas);
    this.canvas.style.width = '100%';
    this.canvas.style.height = '100%';
    this.canvas.width = this.canvas.offsetWidth;
    this.canvas.height = this.canvas.offsetHeight;
  }
  componentWillUnmount() {
    this.canvas.removeEventListener('click', this.press, false);
    this.canvas = null;
    this.color = null;
    this.element = null;
    this.canvasContext = null;
    this.radius = null;
    this.centerX = null;
    this.centerY = null;
  }
  press = (event) => {
    this.color = this.props.color;
    this.element = event.toElement;
    this.element.style.opacity = 0.25;
    this.canvasContext = this.element.getContext('2d');
    this.radius = 0;
    this.centerX = event.offsetX;
    this.centerY = event.offsetY;
    this.canvasContext.clearRect(0, 0, this.element.width, this.element.height);
    this.draw();
  }

  draw = () => {
    this.canvasContext.beginPath();
    this.canvasContext.arc(this.centerX, this.centerY, this.radius, 0, 2 * Math.PI, false);
    this.canvasContext.fillStyle = this.color;
    this.canvasContext.fill();
    this.radius += 2;
    if (this.radius < this.element.width) {
      requestAnimationFrame(this.draw);
    }
    else {
      this.element.style.opacity = 0;
      //this.canvasContext.fillStyle = this.RETCOLOE;
      //this.canvasContext.fill();
    }
  }
  getletChildren = () => {
    let children = this.props.children;
    const _this = this;   
    let snailClassName = _this.props.wrapClassName + '-button';
    return React.cloneElement(children, {
      className: classnames(snailClassName, children.props.className),
      ref: (e) => _this.btn = e
    });
  } 

  render() {
    return this.getletChildren();
  }
}

buttonAnim.less

.snail-button {
  position: relative;
  color: #7d8191;
  overflow: hidden;
  &:hover {
    color: rgba(158, 158, 158, 0.81);
  }
}
.snail-button canvas {
    opacity: 0.25;
    position: absolute;
    top: 0;
    left: 0;
}
chenjf5 commented 7 years ago

Worker案例

onmessage =function(event) { console.log(event.data); }

chenjf5 commented 7 years ago

window.onerror

window.onerror用来捕获js异常,但是如果异常是在try里抛出被catch住window.onerror是不会被触发的