Advertisement
B1KMusic

[Sprite Test] sprite.js

Mar 5th, 2015
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. function checkbox(label, value, parent){
  3.     /* This function generates an HTMLSpanElement to act as the checkbox
  4.      */
  5.     function get_value(){ //..... returns the value, 1 or 0
  6.         return this.value;
  7.     }
  8.     function set_value(value){ // sets value and class accordingly. Accepts 1 (true) or 0 (false)
  9.         this.value = value;
  10.         this.className = "check " + (value ? "" : "un") + "checked";
  11.             //................... That's a ternary switch. It goes like this: boolean ? when true : when false
  12.     }
  13.     function toggle_value(){ // Self-explanatory
  14.         if(this.value) this.set_value(0);
  15.         else this.set_value(1);
  16.     }
  17.     var cb = document.createElement("span"); // creating the 'checkbox': an instance of HTMLSpanElement
  18.     cb.get_value = get_value; //............... Exposing the above functions to cb, such that e.g. cb.toggle_value() could be called from the JS console and trigger the checkbox to toggle
  19.     cb.set_value = set_value;
  20.     cb.toggle_value = toggle_value;
  21.  
  22.     cb.addEventListener("click", toggle_value); // adding toggle_value() to click event
  23.     cb.set_value(value) //........................ defaulting value to what is provided at creation
  24.     cb.label = label; //.......................... setting label, because why not
  25.     parent.appendChild(cb); //.................... this is the line that actually makes the checkbox appear on the webpage
  26.     parent.appendChild((function(){ //............ generate a span containing the label, so that you can see what the checkbox does
  27.         var lb = document.createElement("span");
  28.         lb.innerHTML = label;
  29.         return lb;
  30.     })());
  31.     parent.appendChild(document.createElement("br")); // line break so everything isn't in a single giant line
  32.     return cb; // returning object so that it can be referenced. Otherwise, it would die here, and have to be referenced via document.querySelector()
  33. }
  34.  
  35. var cbs = [
  36.     checkbox("Scruff palm trees", 0, settings),
  37.     checkbox("Save the times when raccoons attack", 1, settings),
  38.     checkbox("when? now?", 1, settings)
  39. ];
  40. /* Notice that I did not use document.querySelector() or document.getElementByID(), or even define "settings" at all. When the script is loaded AFTER the body,
  41.  * the body's ID'd elements are automatically referenced by ID and provided at the window level (as global variables).
  42.  * So, 'settings' is a reference to <div id="settings"></div> from the HTML document that calls this script.
  43.  */
  44.  
  45. function collision(x1,y1,w1,h1,x2,y2,w2,h2){
  46.   /* I like this function. It's simultaneously simple, elegant and complex.
  47.    * It detects a collision between two objects by checking that they are NOT outside of each other.
  48.    */
  49.   return !(
  50.     x1+w1 < x2 ||
  51.     y1+h1 < y2 ||
  52.     x2+w2 < x1 ||
  53.     y2+h2 < y1
  54.   );
  55. }
  56. function report_button(){
  57.     /* Generates the "report button"
  58.      */
  59.     function hover(e){ //......... This toggles the button between its up and hover states.
  60.       if(e.type == "mouseout"){ // If the mouse is leaving the button...
  61.         this.className = "button up";
  62.         return;
  63.       }
  64.       if(e.type == "mouseover"){ // entering...
  65.         this.className = "button hover";
  66.         return;
  67.       }
  68.       /* If control makes it here, then we're dealing with a "mousemove" event. No need to explicitly check,
  69.        *   because the only three events that call this function are mouseover, mouseout, and mousemove
  70.        * The following is to defend against weird stuff that can stop the button from receiving events,
  71.        *   such as an alert() box, a suitably fast mouse hand, and who-knows-what.
  72.        */
  73.       var mx = e.clientX || e.pageX;
  74.       var my = e.clientY || e.pageY;
  75.       if (collision(mx, my, 0, 0, this.offsetLeft+2, this.offsetTop+2, 96-4, 24-4)){
  76.         this.className = "button hover";
  77.       }else{
  78.         this.className = "button up";
  79.       }
  80.     }
  81.     function report(){
  82.       /* This is what happens when the button is clicked.
  83.        * Normally, in a constructor function for a button,
  84.        * I would pass this as an argument to the constructor function, which would execute this on-click
  85.        * but since this is a one-off button for a tech demo...
  86.        */
  87.       var i = 0, str = "";
  88.       while (i < cbs.length){
  89.         str += "\n" + cbs[i].label + ":  " + (cbs[i].get_value() ? "On" : "Off");
  90.         i++;
  91.       }
  92.       alert(str);
  93.     }
  94.     var btn = document.createElement("span"); // So now we start building our element...
  95.     btn.className = "button up"; //............. default state
  96.     btn.addEventListener("mouseover", hover); // a bunch of listeners
  97.     btn.addEventListener("mouseout", hover);
  98.     btn.addEventListener("mousemove", hover);
  99.     btn.addEventListener("click", report); //... the on-click trigger
  100.     document.body.appendChild(btn); //.......... Got lazy here and just shoved it into the document body.
  101.     return btn;
  102. }
  103. var rbtn = report_button();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement