Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

Save Automatically?

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

              
                .lock
  .user-input-wrapper
    .msg-wrapper
      p#msg_register.msg init your color lock (4~8 colors)
      p#msg_input.msg.hide enter your color key
    #user_input.user-input
      //.code.code0
    p#hint.hint hint
  
  #palette_wrapper.palette-wrapper
    - var n = 0
    ol#palette.palette
      while n < 10
        li(class="color n#{n}" data-key=n)
        - n++
    #btn_register.btn REGISTER
    #btn_input.btn.hide UNLOCK
    
              
            
!

CSS

              
                $colors: #F44336 #E91E63 #9C27B0 #673AB7 #3F51B5 #2196F3 #009688 #8BC34A #FFC107 #FF9800
$colors-light: #EF5350 #EC407A #AB47BC #7E57C2 #5C6BC0 #42A5F5 #26A69A #9CCC65 #FFCA28 #FFA726

=color($i, $hover: false)
  background: nth($colors, $i + 1)
  @if $hover
    &:hover
      background: nth($colors-light, $i + 1)
      z-index: 10

html
  box-sizing: border-box
  
*,
*:after,
*:before
  box-sizing: inherit

body
  display: flex
  align-items: center
  width: 100vw
  height: 100vh
  background: radial-gradient(ellipse farthest-side at center, hsl(0, 0%, 100%), hsl(0, 0%, 80%))
  color: hsl(0, 0%, 70%)
  font-family: monaco, monospace

.lock
  display: flex
  align-items: center
  justify-content: center
  width: 100%
  background: hsl(0, 0%, 20%)
  box-shadow: 0 0 30px hsl(0, 0%, 18%) inset

.user-input-wrapper
  width: 300px
  margin-right: 50px
  
.msg-wrapper
  position: relative
  font-size: 1em
  height: 2em
  > .msg
    position: absolute
    top: 0
    left: 0
    margin: 0
    overflow: hidden
    white-space: nowrap
    transition: opacity 0.5s
    &.hide
      opacity: 0    

.user-input
  display: flex
  justify-content: center
  height: 60px
  border-radius: 4px
  background: hsl(0, 0%, 15%)
  box-shadow: 0 5px 10px hsla(0, 0%, 0%, 0.1) inset
  overflow: hidden
  > .code
    flex: 1
    box-shadow: -4px 0 10px -4px hsla(0, 0%, 0%, 0.2)
    transition: flex 0.3s
    @for $i from 0 to 10
      &.code#{$i}
        +color($i)

.hint
  margin: 1em 0 0
  text-align: right
  opacity: 0
  transition: all 0.3s
  pointer-events: none
  &.success
    color: #9CCC65
    opacity: 1
  &.fail
    color: #EF5350
    opacity: 1
  
.palette-wrapper
  position: relative
  display: flex
  justify-content: center
  cursor: pointer
  &.success
    animation: rotation 1s forwards
      
@keyframes rotation
  50%
    -webkit-filter: grayscale(100%)
    filter: grayscale(100%)
  100%
    -webkit-filter: grayscale(0%)
    filter: grayscale(0%)
    transform: rotate(360deg)

.palette
  position: relative
  overflow: hidden
  width: 300px
  height: 300px
  padding: 0
  border-radius: 50%
  list-style: none

.color
  position: absolute
  top: 50%
  left: 50%
  background: #555
  width: 200px
  height: 200px
  transform-origin: 0% 0%
  box-shadow: 0 0 20px hsla(0, 0%, 0%, 0.2)
  transition: all 0.3s
  @for $i from 0 to 10
    &.n#{$i}
      +color($i, true)
      transform: rotate($i * 36deg) skewx(54deg)

.btn
  $size: 100px
  position: absolute
  z-index: 10
  top: 50%
  left: 50%
  width: $size
  height: $size
  margin: #{- $size / 2} 0 0 #{- $size / 2}
  background: hsl(0, 0%, 20%)
  border-radius: 50%
  box-shadow: 0 0 20px -5px hsla(0, 0%, 0%, 0.2)
  line-height: $size
  text-align: center
  transition: all 0.5s
  &:hover
    transform: scale(2.5)
  &.hide
    visibility: hidden
    opacity: 0
              
            
!

JS

              
                const MSG = {
  REGISTERED: {
    failed: false,
    msg: "registered!"
  },
  UNLOCKED: {
    failed: false,
    msg: "unlocked!"
  },
  LENGTH_ERR: {
    failed: true,
    msg: "key length error!"
  },
  WRONG_KEY: {
    failed: true,
    msg: "key incorrect!"
  }
};

class ColorLock {
  
  constructor (config = {max: 8, min: 4}) {
    this.maxlength = config.max;
    this.minlength =config.min;

    this.$palette_wrapper = document.getElementById("palette_wrapper");
    this.$palette = document.getElementById("palette");
    this.$register = {
      msg: document.getElementById("msg_register"),
      btn: document.getElementById("btn_register")
    };
    this.$input = {
      msg: document.getElementById("msg_input"),
      btn: document.getElementById("btn_input")
    }
    this.$user_input = document.getElementById("user_input");
    this.$hint = document.getElementById("hint");
    
    this.init();
    this.bindEvents();
  }
  
  init() {
    this.key = "";
    this.isInit = true;
    this.resetUserInput();
  }
  
  resetLock() {
    this.$register.btn.classList.remove("hide");
    this.$register.msg.classList.remove("hide");
    this.$input.btn.classList.add("hide");
    this.$input.msg.classList.add("hide");
    this.init();
  }
  
  register() {
    if (!this.isInit) {
      this.throwMsg(MSG.REG_FAILED);
      return;
    }
    if (this.userInput.length > this.maxlength || this.userInput.length < this.minlength) {
      this.throwMsg(MSG.LENGTH_ERR);
    } else {
      this.key = this.userInput;
      this.isInit = false;
      this.throwMsg(MSG.REGISTERED);
      this.$register.btn.classList.add("hide");
      this.$register.msg.classList.add("hide");
      this.$input.btn.classList.remove("hide");
      this.$input.msg.classList.remove("hide");
      this.$palette_wrapper.classList.add("success");
    }
    this.resetUserInput();
  }
  
  input () {
    if (this.userInput === this.key) {
      this.resetLock();
      this.throwMsg(MSG.UNLOCKED);
      this.$palette_wrapper.classList.add("success");
    } else {
      console.log(`your key is ${this.key}`);
      this.throwMsg(MSG.WRONG_KEY);
    }
    this.resetUserInput();
  }
  
  throwMsg (status) {
    this.$hint.textContent = status.msg;
    if (status.failed) {
      this.$hint.classList.add("fail");
    } else {
      this.$hint.classList.add("success");
    }
    setTimeout(() => {
      this.$hint.classList.remove("success", "fail")
    }, 1000);
  }
  
  bindEvents () {
    let _register = e => this.register(e),
        _input = e => this.input(e),
        _setUserInput = e => this.setUserInput(e),
        _keypressManager = e => this.keypressManager(e);
    
    this.$register.btn.addEventListener("click", _register);
    this.$input.btn.addEventListener("click", _input);
    this.$palette.addEventListener("click", _setUserInput);
    document.body.addEventListener("keypress", _keypressManager);
    this.$palette_wrapper.addEventListener("animationend", () => {
      this.$palette_wrapper.classList.remove("success");
    });
  }
  
  resetUserInput () {
    this.userInput = "";
    while(this.$user_input.firstChild) {
      this.$user_input.removeChild(this.$user_input.firstChild);
      
    }
  }
  
  setUserInput (e) {
    let key;
    if (e.target.dataset.key) {
      key = String(e.target.dataset.key);
    } else {
      key = String.fromCharCode(e.which);
    }
    this.userInput += String(key)
    this.$user_input.appendChild(this.getUserInputHtml(key));
  }
  
  getUserInputHtml (n) {
    let dom = document.createElement("div");
    dom.className = `code code${n}`;
    return dom;
  }
  
  keypressManager (e) {
    if (e.which > 47 && e.which < 58) {
      this.setUserInput(e);
    } else if (e.which === 13) {
      if (this.key) {
        this.input();
      } else {
        this.register();
      }
    } else if (e.which === 99) {
      this.resetLock();           
    }
  }
}

var colorLock = new ColorLock();
              
            
!
999px

Console