Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

feat:add pure color props type , change options [image] => [cover] #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lijinke666
Copy link

@lijinke666 lijinke666 commented Aug 10, 2017

Hello :)

your code options only drawImage accept a image props option

i change the image props to cover It turns it into two different types of parameters

Not only image cover String, Can also be use hexadecimal coding or rgba()

like #396 or rgba(255,255,255,.3)

please see index.js

  componentDidMount() {
    const { cover } = this.props
    this.isDrawing = false;
    this.lastPoint = null;
    this.ctx = this.canvas.getContext('2d');

    const isColorCover = this.checkColorCover(cover)

    if (!isColorCover) {
      const image = new Image();
      image.crossOrigin = "Anonymous";
      image.onload = () => {
        this.ctx.drawImage(image, 0, 0);
        this.setState({ loaded: true });
      }
      image.src = cover;
    } else {
      const { width, height } = this.canvas
      this.ctx.save()
      this.ctx.fillStyle = cover
      this.ctx.beginPath()
      this.ctx.rect(0, 0, width, height)
      this.ctx.fill()
      this.ctx.restore()
      this.setState({ loaded: true });
    }

  }
  checkColorCover(cover) {
    return (/^#(\d|\w){3,6}$/.test(cover) || /^rgba?\(.*\)/.test(cover))
  }

And I made a little bit of improvement

i add a new package classnames in your code

This allows the user to expand his own like className , style

    return (
      <div
        className={classnames("ScratchCard__Container", className)}
        style={containerStyle}
        {...attr}
      >
        <canvas {...canvasProps}></canvas>
        <div className="ScratchCard__Result" style={resultStyle}>
          {this.props.children}
        </div>
      </div>
    );

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant