2

What I've got so far up on codepen:

http://codepen.io/Shane24/pen/cEGsb

I need to save the values that appear in the input boxes when you move the sliders into variables.

Any help greatly appreciated. Thanks.

7
  • Which part are you having difficulties with? This is really simple. Please don't just ask for people to write the code for you mattgemmell.com/2008/12/08/what-have-you-tried Commented Mar 20, 2013 at 21:54
  • Just store 3 global variables in JavaScript and add listeners to the sliders to change those values and use clearRect followed by a redrawing of the canvas and you're all set. Should be peanuts really. Use your imagination :) Commented Mar 20, 2013 at 21:55
  • @JuanMendes I'm not asking you to write it? I'm having difficulties getting the numbers that appear in the input box saved into variables which update dynamically using the sliders Commented Mar 20, 2013 at 21:59
  • @Allendar Thanks. Not sure how to add a listener but I'll give it a go. Appreciated! Commented Mar 20, 2013 at 22:00
  • @Shane A good learning school is to look in to the Development version of jQuery. I'm not promoting to "use" jQuery here, but it's source code has tons of cool raw JavaScript gems. Look into jQuery's change() functionality; api.jquery.com/change Good luck! Commented Mar 20, 2013 at 22:02

1 Answer 1

2

The simplest answer to the question is that you can set your variables from the slider's handlers

 slide: function( event, ui ) {
    $('amount1').val( ui.value );
    // This is the value that you just dragged the slider to, there it is,
    // in a variable
    x = ui.value;
  }

Without knowing what you needed I whipped up an example that should show you a working example where I extract the values from the sliders and apply it to the color of the circle in the canvas. This code works with the HTML you posted initially

function drawOnCanvas () {
   var rgb =[
       $('#slider-1').slider("value"),
       $('#slider-2').slider("value"),
       $('#slider-3').slider("value")
   ];
   var c = document.getElementById("myCanvas2");
   var ctx = c.getContext("2d");
   var color = "rgb("+ rgb.join(',') + ")" ;
   ctx.fillStyle = color;
   ctx.beginPath();
   ctx.arc(100, 100, 100, 0, Math.PI*2, true);
   ctx.closePath();
   ctx.fill();
}

function createSlider(slider, boundTextField) {
   slider.slider({
      orientation: "vertical",
      range: "min",
      min: 0,
      max: 255,
      value: 0,
      slide: function( event, ui ) {
        boundTextField.val( ui.value );
        drawOnCanvas();
      }
  })
}

$(function() {
    createSlider($( "#slider-1" ), $( "#amount1" ));
    createSlider($( "#slider-2" ), $( "#amount2" ));
    createSlider($( "#slider-3" ), $( "#amount3" ));    
});
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. I'm getting a console output of "Uncaught TypeError: Cannot set property 'onchange' of null"
I added the button <input type="button" id="amount1" /> and added var textfield = document.getElementById('amount1'); var value = textfield.value; document.write(value); to my js. However the document.write prints nothing. Any idea what I'm doing wrong? Also I have slide: function( event, ui ) { z = ui.value; $( "#amount1" ).val( ui.value ); } - Is that even doing anything? As you can see, my javascript is not the best...
Don't ever use document.write, what you were doing wrong is that you were executing the code before the HTML was parsed. That last snipper is setting the value in the text field. Hopefully my new answer will help you, but you need to read up a lot more and ask more detailed questions that show what you have tried. This question did not show what you've tried, because you didn't even describe what you wanted (Setting variables doesn't count)
Thanks. I didn't know that about document.write. That's a huge help. To give you some context, I have a pair of headphones drawn in HTML5 canvas. I currently have the functionality for the user to rotate the headphones, but I needed to be able to allow the user to also change the colour via a slider. I decided to start small with the circle. I'm new to javascript, and stack overflow, so apologies if I wasn't clear, or if it was too simple a question to be asking. I greatly appreciate your help. Thanks so much! The new example is exactly the functionality I was looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.