Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.1/react.min.js"></script>  
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.1/react-dom.min.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/konvajs/konva/0.14.0/konva.min.js"></script>
<script type="text/javascript" src="https://rawgit.com/lavrton/react-konva/v1.0.6/dist/react-konva.bundle.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/bgrins/TinyColor/master/dist/tinycolor-min.js"></script>

<div id='container'></div>
              
            
!

CSS

              
                body {
  background: gray;
}

#container {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

#background {
  display:flex;
  justify-content: center;
  align-items: center;
}

#stage {
  border: 5px solid darkgrey;
  border-radius: 10px;
  background-color: #EBEBEB;
  box-shadow: 0px 0px 20px black;
}
              
            
!

JS

              
                const {Layer, Stage, Circle} = ReactKonva;

class ColorWheel extends React.Component {
  constructor(){
    super()
    this.state = {
      hue: 270,
      saturation: 100,
      light: 50,
      color: 'hsl(270,100%,50%)'
    }
    this.getHue = this.getHue.bind(this)
    this.getSaturation = this.getSaturation.bind(this)
    this.getLight = this.getLight.bind(this)
    this.hueDragBound = this.hueDragBound.bind(this)
    this.saturationDragBound = this.saturationDragBound.bind(this)
    this.lightDragBound = this.lightDragBound.bind(this)
    this.drawHueSlider = this.drawHueSlider.bind(this)
    this.drawSaturationSlider = this.drawSaturationSlider.bind(this)
    this.drawLightSlider = this.drawLightSlider.bind(this)
    this.drawSample = this.drawSample.bind(this)
  }
  
  drawSample(){
    var canvas = this.refs.layer.canvas;
    var ctx = canvas.getContext("2d");
    
    var color = tinycolor(this.state.color)
    var hex = '#' + color.toHex().toUpperCase()
    
    ctx.clearRect(95,85,110,130)
    ctx.fillStyle = 'lightgray'
    ctx.fillRect(95,85,110,130)
    ctx.fillStyle = this.state.color
    ctx.fillRect(100,90,100,100);
    
    ctx.font = "20px sans-serif";
    ctx.fillStyle = 'gray'
    ctx.fillText(hex, 107, 210);
  }
  
  getHue(x,y) {
    var angleDeg = Math.floor((Math.atan2(150 - y, 150 - x) * 180 / Math.PI) + 180);
    this.setState({
      hue: angleDeg,
      color: 'hsl('+angleDeg+','+this.state.saturation+'%,'+this.state.light+'%)'
    })
    this.drawSaturationSlider()
    this.drawLightSlider()
    this.drawSample()
  }
  
  getSaturation(x,y) {
    var offset = 50;
    var height = 200;
    var saturation = Math.round(-y/2 + 125)
    this.setState({
      saturation: saturation,
      color: 'hsl('+this.state.hue+','+saturation+'%,'+this.state.light+'%)'
    })
    this.drawHueSlider()
    this.drawLightSlider()
    this.drawSample()
  }
  
  getLight(x,y) {
    var offset = 50;
    var height = 200;
    var light = Math.round(-y/2 + 125)
    this.setState({
      light: light,
      color: 'hsl('+this.state.hue+','+this.state.saturation+'%,'+light+'%)'
    })
    this.drawHueSlider()
    this.drawSaturationSlider()
    this.drawSample()
  }
  
  saturationDragBound(pos){
    var top = 50
    var height = 200
    var bottom = height + top
    return {
      x: 365,
      y: pos.y < top? top : pos.y> bottom? bottom : pos.y 
    }
  }
  
  hueDragBound(pos){
    var x = 150;
    var y = 150;
    var radius = 110;
    
    if (pos.x === x && pox.y === y) return {x: x + radius, y: y}
    else {
        var scale = radius / Math.sqrt(Math.pow(pos.x - x, 2) + Math.pow(pos.y - y, 2));
      return {
        y: Math.round((pos.y - y) * scale + y),
        x: Math.round((pos.x - x) * scale + x)
      }
    }
  }
  
  lightDragBound(pos){
    var top = 50
    var height = 200
    var bottom = height + top
    return {
      x: 445,
      y: pos.y < top? top : pos.y> bottom? bottom : pos.y 
    }
  }
  
  drawLightSlider(){
    var canvas = this.refs.layer.canvas;
    var ctx = canvas.getContext("2d");
    ctx.clearRect(425,0,40,300)
    
    ctx.beginPath()
    ctx.arc(445,50,15,0,360,true)
    ctx.fillStyle = 'hsl('+this.state.hue+','+this.state.light+'%,100%)';
    // ctx.fillStyle = 'hsl(0,0%,100%)';
    ctx.fill()
    
    ctx.beginPath()
    ctx.arc(445,250,15,0,180,false)
    ctx.fillStyle = 'hsl('+this.state.hue+','+this.state.light+'%,0%)';
    // ctx.fillStyle = 'hsl(0,0%,0%)';
    ctx.fill()
    
    for(var i=0;i<=100;i++) {
      ctx.fillStyle = 'hsl('+this.state.hue+','+this.state.saturation+'%,'+i+'%)';
      // ctx.fillStyle = 'hsl(0,0%,'+i+'%)';
      ctx.fillRect(430,250-2*i,30,2);
    }
  }
  
  drawSaturationSlider(){
    var canvas = this.refs.layer.canvas;
    var ctx = canvas.getContext("2d");
    ctx.clearRect(350,0,40,300)
    ctx.beginPath()
    ctx.arc(365,50,15,0,360,true)
    // ctx.fillStyle = 'hsl('+this.state.hue+',100%,'+this.state.light+'%)';
    ctx.fillStyle = 'hsl('+this.state.hue+',100%,'+'50%)';
    ctx.fill()
    
    ctx.beginPath()
    ctx.arc(365,250,15,0,180,false)
    // ctx.fillStyle = 'hsl('+this.state.hue+',0%,'+this.state.light+'%)';
    ctx.fillStyle = 'hsl('+this.state.hue+',0%,'+'50%)';
    ctx.fill()
    
    for(var i=0;i<=100;i++) {
      // ctx.fillStyle = 'hsl('+this.state.hue+','+i+'%,'+this.state.light+'%)';
      ctx.fillStyle = 'hsl('+this.state.hue+','+i+'%,'+'50%)';
      ctx.fillRect(350,250-2*i,30,2);
    }
  }
  
  drawHueSlider(){
    var canvas = this.refs.layer.canvas;
    var ctx = canvas.getContext("2d");
    var x = 150;
    var y = 150;
    var radius = 125;
    var counterClockwise = false;
    
    ctx.clearRect(0,0,300,300)
    
    for(var angle=0; angle<360; angle++){
        var startAngle = (angle-1)*Math.PI/180;
        var endAngle = (angle+1) * Math.PI/180;
        ctx.beginPath();
        ctx.moveTo(x, y);
        ctx.arc(x, y, radius, startAngle, endAngle, counterClockwise);
        ctx.closePath();
        // ctx.fillStyle = 'hsl('+angle+','+this.state.saturation+'%,'+this.state.light+'%)';
      ctx.fillStyle = 'hsl('+angle+','+'100'+'%,'+'50'+'%)';
        ctx.fill();
    }

    ctx.beginPath();
    ctx.arc(x,y,radius,0,360)
    ctx.closePath();
    
    ctx.beginPath();
    ctx.arc(x,y,radius-30,0,360)
    ctx.closePath();
    ctx.fillStyle = '#EBEBEB'
    ctx.fill();
  }
  
  componentDidMount(){
    this.drawHueSlider()
    this.drawSaturationSlider()
    this.drawLightSlider()
    this.drawSample()
  }
  
  render(){
    
    return (
      <div id='background' style={{background: 'grey'}}>
      <div id='stage'>
      <Stage width={500} height={300} >
        <Layer ref='layer' />
        <Layer>
          <Selector getColor={this.getHue} color={this.state.color} dragBound={this.hueDragBound} x={150} y={40} />
        </Layer>
        <Layer>
          <Selector getColor={this.getSaturation} color={this.state.color} dragBound={this.saturationDragBound} x={365} y={50} />
        </Layer>
        <Layer>
          <Selector getColor={this.getLight} color={this.state.color} dragBound={this.lightDragBound} x={445} y={150} />
        </Layer>
      </Stage>
        </div>
        </div>
    )
  }
}

class Selector extends React.Component {
  constructor(){
    super()
    this.onDrag = this.onDrag.bind(this)
  }
  
  componentDidMount(){
    this.refs.circle.attrs.x = this.props.x
    this.refs.circle.attrs.y = this.props.y
  }
  
  onDrag(e){
    var _x = e.target.attrs.x
    var _y = e.target.attrs.y
    this.props.getColor(_x,_y)
  }

  render(){
    return (
      <Circle ref='circle'
        radius={20} stroke='white' strokeWidth={1} fill={this.props.color} shadowBlur={4} draggable
        onDragStart={this.onDrag} onDragMove={this.onDrag}
        dragBoundFunc={this.props.dragBound}
        transformsEnabled={'position'}
      />
    )
  }
}

ReactDOM.render(<ColorWheel />, document.getElementById('container'))


              
            
!
999px

Console