Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    3
    Thanked 1 Time in 1 Post

    Stop script form changing a variable

    I'm applying effects/filters to imageData with this script and my code:
    Code:
    var data = variableIGetTheImageDataFrom;
    JSManipulate.bump.filter(data, {});
    The script changes the imageData of the data variable to the modified imageData but it also changes the imageData of the variableIGetTheImageDataFrom to the same value.
    However I don't want the variableIGetTheImageDataFrom to change, any idea on how to achieve this?

    Thank You!

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,172
    Thanks
    203
    Thanked 2,549 Times in 2,527 Posts
    Create another variable to hold the changed value.

    Code:
    var data = variableIGetTheImageDataFrom;
    var data1 = data;
    JSManipulate.bump.filter(data1, {});

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New Coder
    Join Date
    Jun 2012
    Posts
    80
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Hi DeadFred,
    You can use read only property or function to do that, I wrote some example here.

    Code:
    function Keeper(Default){ 
    var _Value = Default; 
    this.GetValue = function() { return _Value } 
    }
    
    var PI = new Keeper( 22/7 );
    var Circle = PI.GetValue() * Math.pow(5,2);
    Oh, I forgot to mention it, you can prevent changing from primitive type variable only (like number or chars) other then that will not.
    So if your variableIGetTheImageDataFrom isn't primitive type you have to recreate it every time to prevent a changing.

  • Users who have thanked Richter for this post:

    DeadFred (07-11-2012)

  • #4
    New Coder
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    3
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Philip M View Post
    Create another variable to hold the changed value.

    Code:
    var data = variableIGetTheImageDataFrom;
    var data1 = data;
    JSManipulate.bump.filter(data1, {});

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Tried this one myself as well didn't work

  • #5
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,248
    Thanks
    15
    Thanked 254 Times in 254 Posts
    Quote Originally Posted by DeadFred View Post
    Tried this one myself as well didn't work
    What Philip M suggested will work, I do it all the time. You just haven't implemented it right.

  • #6
    New Coder
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    3
    Thanked 1 Time in 1 Post
    Same here But it doesn't I'll provide some more code on stuff that might me causing it:

    I have a few images in a canvas if you click on em you select em and can apply a filter or move them (using KineticJS for most of this). I want you to apply 1 filter at a time instead of stacking the filters, so when i load the image into the canvas I store the imageData:
    Code:
    imageCanvas.width = img.width;
    imageCanvas.height = img.height;
    imageCanvasContext.drawImage(img, 0, 0);
    Kinetic.Image.prototype.imageData = "";
    kimgObj.imageData = imageCanvasContext.getImageData(0, 0, img.width, img.height);
    (where imageCanvas is the canvas i use to get the imageData, img the imageObject and kimbObj the kineticImage object)

    Then when you press the image you get to choose from a few filters when you click on a option it will apply that filter, right now it stacks the filters instead of applying it to the fresh image every time so I get the fresh imageData that i stored in the kineticImage object place that value in a data variable and run that through the JSManipulate function. However the clean image value that I stored in the kineticImage object changes aswell as the data value that I use to display the modified image.

    Hope you guys have any clues.

    Ps. If needed I can just post the entire source code, though its kinda messy and doesn't contain to many comments.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,720
    Thanks
    80
    Thanked 4,514 Times in 4,478 Posts
    Better than posting the code, post a live URL for us to look at.

    I *SUSPECT* the problem is that you aren't cloning you object. That is, you only have one object, and so the filters indeed keep changing the one and only object.

    When you copy an object reference in JavaScript (and in Java and in C and in C++ and...) you are *ONLY* copying the *REFERENCE*, not the object. Which means that now you simply have two (or more!) variables that point to the SAME object. The only way to create a second object is to indeed clone the first one (or create a second object and copy all the characteristics of the first one to the second one, but that's just another way to clone).

    Here is a simple example to demonstrate what I mean:
    Code:
    <script type="text/javascript">
    var a = new Object();
    a.name = "This is object a";
    var b = a;
    alert( "b's name is " + b.name );
    b.name = "This is object b";
    alert( "a's name is " + a.name );
    </script>
    See what I mean?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    DeadFred (07-11-2012)

  • #8
    New Coder
    Join Date
    Jun 2012
    Posts
    80
    Thanks
    0
    Thanked 18 Times in 18 Posts
    @Old Pedant,
    That what I try to tell him but seem like no one read my post.
    If your object isn't a primitive type like number or characters, you can't prevent them to change object's members value.

    JavaScript : By Value vs. By Reference
    http://docstore.mik.ua/orelly/web/jscript/ch09_03.html
    Last edited by Richter; 07-11-2012 at 11:13 AM.

  • #9
    New Coder
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    3
    Thanked 1 Time in 1 Post
    Thank You guys Fixed it:
    Code:
    //Setting the side of the canvas i use for imageData stuff
    imageCanvas.width = oldImageData.width;
    imageCanvas.height = oldImageData.height;
    
    //Clearing the canvas and creating a fresh imageData
    imageCanvasContext.clearRect(0, 0, imageCanvas.width, imageCanvas.height);
    var data = imageCanvasContext.createImageData(oldImageData.height, oldImageData.width);
    
    //Placing the old imageData values into the new fresh one	
    for (var i = 0; i < oldImageData.data.length; ++i) {
    data.data[i] = oldImageData.data[i];
    }
    Last edited by DeadFred; 07-11-2012 at 12:29 PM.

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,720
    Thanks
    80
    Thanked 4,514 Times in 4,478 Posts
    Quote Originally Posted by Richter View Post
    @Old Pedant,
    That what I try to tell him but seem like no one read my post.
    I think he read it but didn't understand it. That's why I gave an example. Examples usually work better than words.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •