...

View Full Version : Stop script form changing a variable



DeadFred
07-10-2012, 03:49 PM
I'm applying effects/filters to imageData with this (https://github.com/JoelBesada/JSManipulate/blob/master/script/jsmanipulate.js) script and my 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!

Philip M
07-10-2012, 05:30 PM
Create another variable to hold the changed value.


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.

Richter
07-10-2012, 07:37 PM
Hi DeadFred,
You can use read only property or function to do that, I wrote some example here.


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.

DeadFred
07-10-2012, 10:02 PM
Create another variable to hold the changed value.


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

DrDOS
07-10-2012, 10:22 PM
Tried this one myself as well didn't workWhat Philip M suggested will work, I do it all the time. You just haven't implemented it right.

DeadFred
07-10-2012, 11:46 PM
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:


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.

Old Pedant
07-11-2012, 02:56 AM
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:


<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?

Richter
07-11-2012, 07:31 AM
@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

DeadFred
07-11-2012, 01:26 PM
Thank You guys Fixed it:


//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];
}

Old Pedant
07-11-2012, 08:26 PM
@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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum