Passing a color through a variable in a canvas animation

Sam Mcleod

I am attempting to make a customisable animation on a canvas.

I am trying to change the colour of the animation with the click of a button by passing through a different rgb value.

At the moment I am not getting any response to the click of the button

http://codepen.io/smbazalo/pen/RPrQRN

html

  <button onclick="changedotColor('rgb(63,169,245)')"  ><img  id="blue" class="swatch"></button>

js

var dotcolor_val = "rgb(6,250,212)";
function changedotColor(input_color){
dotcolor_val = input_color;
console.log(input_color)
}

function Circle( args ) {
this.position  = [ 0, 0 ];
this.angle     = 30;
this.speed     = dotSpeed;
this.offset    = 1;
this.length    = 80;
this.size      = 5;
this.color     = dotcolor_val;
this.direction = 'grow';
}

Any advice on what could make this work?//Why this isn't working currently?

Much appreciated

Bassem

You are changing the local variable dotcolor_val via the function changedotColor(), however, this variable's change of value is not being reflected in the Circle instances.

This is the link to the working code: http://codepen.io/anon/pen/RPrQVW

Explanation

In your code you are defining an object Circle with function Circle(args) with a few properties.

You are instantiating and pushing these Circle objects into 3 lists with the following code:

for (i = 1; i < 10; i++) {
  var offset = 1;
  rowOne.push(new Circle({
    angle: 0,
    offset: i
  }));
  rowTwo.push(new Circle({
    angle: 120,
    offset: i
  }));
  rowThree.push(new Circle({
    angle: 240,
    offset: i
  }));
}

rowOne, rowTwo and rowThree contain the Circle objects you have created. Thus, one way to change the color of your created Circles is to do 2 things:

  1. Define a new function in the Circle object that will change the color of the object, as such:

    Circle.prototype.changeColor = function( rgbcolor ) {
        this.color = rgbcolor;
    }
    
  2. When you trigger the function changedotColor() you need to iterate over the 3 lists and their children and trigger the changeColor function, as such:

    function changedotColor(input_color) {    
      for (i = 0; i < 3; i++) {
        rowOne[i].changeColor(input_color);
        rowTwo[i].changeColor(input_color);
        rowThree[i].changeColor(input_color);
      }
    }
    

A different approach would be to modify your render method to take into consideration instance variable changes i.e. color value of the Circle object.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Passing variable through classes

From Dev

Passing a variable through _GET

From Dev

Variable not passing through?

From Dev

Passing through variable example

From Dev

passing through and outputting value to console but not drawing on the canvas

From Dev

Change background color of JavaScript canvas animation

From Dev

How to change the color of an animation in a canvas by using javascript?

From Dev

Variable value not passing through loop

From Dev

Zend: Passing variable through redirect

From Dev

Passing a string variable through ajax

From Dev

Passing an Environment Variable Through Alacarte?

From Java

Passing a variable through a method in Java

From Dev

Passing variable in pages through URL

From Dev

Passing an object variable through html

From Dev

Passing variable through pages in WordPress

From Dev

PHP: passing a variable through a link

From Dev

Passing a variable through the form action

From Dev

Angularjs Passing variable through resolve function

From Dev

Simple function not passing through the variable correctly

From Dev

Passing variable through URL with angular js

From Dev

Passing a variable to javascript through HTML Element

From Dev

Custom menu Passing a WorkSheet variable through .OnAction

From Dev

Passing a Variable through NSStatusItem's Action

From Dev

Issue with passing a variable through prepareForSegue method

From Dev

Passing variable to next page through url

From Dev

Passing a context variable through a class in Django

From Dev

Returning the updated variable passing through private methods

From Dev

Passing a JavaScript variable to the view through a HTML form

From Dev

Passing defined variable through different scenarios is not working

Related Related

HotTag

Archive