Is it possible to adjust an object's property later on?

Working with KineticJs, I aim to efficiently handle elements by creating objects for easy management.

Below is my code which is not functioning as expected. (I am relatively new to object-oriented programming and still learning)

 $(function(){     
    var stage = new Kinetic.Stage({
        container: 'gridalea',
        width: 172,
        height: 720
    });

    var color_layer = new Kinetic.Layer();
    var sh_layer = new Kinetic.Layer();

    var Left={
        light: {
            frame: function(){
                poly = new Kinetic.Polygon({
                    points: [70, 0, 0, 22, 0, 672, 70, 720, 70, 710, 6, 667, 6, 29, 70, 10, 70, 0],
                    stroke: 'white',
                    strokeWidth: 1
                });
                // add the shape to the layer
                color_layer.add(poly);
                stage.add(color_layer); 
            },
            fill: function(color){
                poly.setFill(color);
            }
        },
        dark: {
            frame: function(){
                var poly = new Kinetic.Polygon({
                    points: [6, 193, 6, 667, 11, 663, 11, 440, 6, 437, 6, 427, 11, 425, 11, 193, 6, 193],
                    stroke: 'white',
                    strokeWidth: 1
                });
                // add the shape to the layer
                color_layer.add(poly);
                stage.add(color_layer); 
            },
            fill: function(color){
                poly.setFill(color);
            }
        },
    }
    Left.light.frame();
    Left.light.fill('red');
});

My objective is to change the fill color of polygons after creation using objects for simplification. Could you possibly identify any issues in my code? Your guidance would be greatly appreciated.

EDIT: Upon adding

Left.dark.frame();
Left.dark.fill('red');

after

Left.light.frame();
Left.light.fill('red');

the fill color changes for Left.light.frame() but not for Left.dark.frame()

Answer №1

One issue I've encountered is needing to add a layer to the stage after it has already been filled.

stage.add(color_layer);

This action must be performed at the very end of the process.

Answer №2

Make sure to include either color_layer.draw() or stage.draw() in your fill function for it to work correctly.

Explanation of how your recent changes fixed the issue:

By calling Left.dark.frame() after Left.light, you are adding another object to the layer and attempting to add the layer back to the stage. This action triggers the draw() method, most likely when adding the layer back to the stage. This refreshes the canvas and results in the color being filled for the Light polygon.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

JQuery HTML not functioning properly with dynamically inserted elements

Initially, I perform this action var columns = row.getElementsByTagName('td'); columns[2].innerHTML ='<Button class="btn btn-success" style="margin-right:4px;" onclick="savecategory(this.parentNode,this)"> ...

Altering the input type of a cloned element in Internet Explorer results in the loss of the value

When I have checkbox inputs displayed in a modal with preset value attributes, upon clicking "OK", I clone them and change their input types to hidden, then append them to a div in the document body. However, when trying to retrieve their values using jQue ...

What is the best way to access the image source attribute within a directive?

Here's a straightforward image directive example: (function () { 'use strict'; angular .module('myapp') .directive('imageTest', imageTest); function imageTest() { var directive = { ...

What is the best method for retrieving objects from a Meteor collection?

I'm a beginner with Meteor and I'm currently working on the introductory tutorial. However, I'm facing difficulties in retrieving data from a collection. Below is my JavaScript code: import angular from 'angular'; import angularM ...

Generating random numbers within specified character limits using Javascript and Selenium

Just starting to learn javascript - I am working on creating random user IDs for my selenium test scenarios. The line of code I am using for the value is as follows: javascript{Math.floor(Math.random()*11111)} However, there is a specific field that need ...

Having trouble retrieving data sent via ajax in PHP

Currently, I am using Ajax to send a variable in my PHP file. Here's the code snippet: getVoteCount: function(){ App.contracts.Election.deployed().then(function(instance) { for(i=0; i<4; i++){ instance.candidates(i).then(functi ...

What is preventing me from loading a module using a variable containing a string?

This code snippet demonstrates how the module can be successfully loaded using import('test'), but not with the second example. These lines of code are executed within an Angular 9 application utilizing the default Webpack configuration. var tes ...

Bye bye validation for cancel button in AngularJS

I am striving to implement a specific feature in my Angular application. I want to have editable form inputs, where a user can view their first name fetched from the server and then switch to an edit mode by clicking a button. The edit button transforms in ...

Invoke a React component within a conditional statement

There is a function for exporting data in either csv or xls format based on an argument specifying the type. The functionality works flawlessly for xls but encounters issues with csv. Here's the code snippet: const exportFile = (exportType) => { ...

Generate a dynamic regular expression using regex characters

If I am looking to find matches for the following files: /foo/bar/baz/x /foo/bar/baz/y/z I can create a regular expression like this: new RegExp('^/foo/bar/baz/.*') But the question arises - how can I instruct the RegExp constructor to interp ...

Create a PDF document using HTML code stored on a separate component within an Angular 2 application

I am facing an issue with my parentComponent and childComponent setup. Specifically, I have a button called downloadPDF in the parentComponent that is supposed to trigger a PDF download from the HTML code of the childComponent without displaying the childC ...

Several examples of objects utilizing the identical function as the most recent instance

While working on a new feature for a Javascript library, I ran into an interesting issue. It seems that when a function of an object utilizes a closure and a promise together, all instances of the object end up using the most recently created version. This ...

Real-world demonstration of multiple screens using three.js for an immersive virtual experience

Check out these awesome examples of multiscreen capabilities: webgl_multiple_canvases_circle webgl_multiple_canvases_complex webgl_multiple_canvases_grid These examples even reference the Google Liquid Galaxy project: liquidGalaxy I am exploring how t ...

Identify Bootstrap modal to modify destination of keypress input

I am working on a piece of code that detects keypress events and focuses input towards a searchbar with the id "search". $( document ).keypress(function() { $("#search").focus(); In addition, I have a bootstrap modal dialog containing input fields ...

The values coming from my cascading combo boxes are incorrect

My form contains drop down boxes with options that dynamically change the values of the next drop down box based on the selection. When I choose the first training option, the dates are successfully sent through the form to my email address. However, i ...

Vue.JS: The central layout element and navigation system

I have a primary layout component (consists of a fixed top header and a left side menu with multiple links created using the router-link component) as well as a "dynamic" layout component that serves as the heart of the page. My goal is to change the cen ...

Having trouble installing the 'ws' npm package on MacOS Big Sur?

Every time I try to install the websocket package using "npm install ws", I keep getting this error message: npm ERR! code ENOSELF npm ERR! Refusing to install package with name "ws" under a package npm ERR! also called "ws". Did you name your project the ...

Delay the execution using Javascript/jQuery, remove the last element from the DOM

Within a jQuery AJAX call, I am using a $.each() loop. Each element in the response data triggers the addition of a portion of HTML to a container class in my index.html file. To view the code and ensure proper formatting, visit the codepen here as the pa ...

Customizing Javascript for Mouse Exiting Browser Window

I have implemented a JavaScript function on my website to display a lightbox when the visitor's mouse goes beyond the browser window. You can check it out here: [http://mudchallenger.com/index-test2.html][1] However, there seems to be an issue where ...

The Highchart Angular directive is failing to update when receiving new data from a dynamic (ajax) data table

My AngularJs application fetches data from an API and populates a table with it. I then use Highcharts to create a chart based on the table data. Everything works fine when I use static data in the table, but it doesn't update properly when I try to c ...