Sg2.9
Problems with slider and func...
Recent RSS
Problems with slider and function call (e.g. scaling)
From:  Axel
Date:  2. October 2010, 02:21

Hi,

in the onChange-part of a slider i want to call a function, let's say scaleShapes(value).

i would do it this way:

(...)
onChange: function(value) { scaleShapes(value) }

(...)

(scaleShape simply calls the scale-method of different Paths passing the value)

but i can't see any change on the shapes i can see in the document.

But, as soon as, i click cancel or ok, scaleShapes is called with the value of the last change.

Is this issue known? Is there a way to get this working?

Axel

Re: Problems with slider and function call (e.g. scaling)
From:  Axel
Date:  2. October 2010, 02:37

the print outs tell me that there should have been a scaling, for example:

function scaleShapes(value) {

shape1.scale(value);
shape2.scale(value);
print("scaling done");

}

gives me the two messages on the console according to every change on the slider. so i dont understand why the shapes dont change.

Axel

Re: Problems with slider and function call (e.g. scaling)
From:  Jürg Lehni
Date:  2. October 2010, 09:09

It would be good to print out the actual value, so you can see what is passed to the scale function.

Also, note that scaling the shape first for example by 0.5 (50%) and then by 0.2 (20%) will not result in it appearing at 20%, as your code suggests. It will be at 20% of 50%, so 10% of its original size. The scale function is not the same as for example the scale properties in Flash. Calling it repeatedly scales incrementally, each time assuming the current state as 100%.

Re: Problems with slider and function call (e.g. scaling)
From:  Axel
Date:  2. October 2010, 10:45

thanks for your help!

ok, i just tried to keep it simple in the example above, because i think that the problem really lies in the scaling part, or update the document part.
i know about the incremental scaling and deal with it in this way:

// global variables start
scaling = 0.0;

// gets filled somewhere else
shapes = new Array();

// global variables end

function createSettingsDialog() {
var settingsDialog = {
(...)
scaling: { type: "slider", label: "scaling size", value: scaling, range:[0,50], onChange: function(value) {
scaleShapes(scaling, true);
scaleShapes(value, false);

}
}

var values = Dialog.prompt("Settings", settingsDialog);
}

(... somewhere a call to createSettingsDialog)

and the scaleShapes Function:

function scaleTriangles(factor, rescale) {

for(i in shapes) {
shape = shapes[i];

if(shape != null) {

if(rescale) {
scaling = 0.0;
shape.scale(1/(1+(factor/100)));
}
else {
scaling = factor;
shape.scale(1+(factor/100));
}
}
}

}

so values all lie within 0 and 50, that is what the print outs tell as well.
and the incremental scaling is avoided by scaling it down to original size first and the to the new size.

thanks your help, but i think the problem lies somewhere else
Any further ideas?

Axel

Re: Problems with slider and function call (e.g. scaling)
From:  Axel
Date:  2. October 2010, 10:57

i also have a issue with the line numbers in error codes. in a nutshell: i dont get any.

see here: http://scriptographer.org/forum/wish-list/line-numbers-in-error-messages/

maybe this is all connected in some weird way

Re: Problems with slider and function call (e.g. scaling)
From:  Axel
Date:  2. October 2010, 11:20

hi,

i wrote a short test. you can check it out if it runs at your setup.

the issue is still the same. as soon as i click "ok" everything scales (and scales correctly). but it doesnt react on the dragging of the slider, but the print outs do.

Axel

Re: Problems with slider and function call (e.g. scaling)
From:  Axel
Date:  2. October 2010, 11:24

by the way:
it doesn't matter whether i click "ok" or "cancel". it scales then to the slider value.

Re: Problems with slider and function call (e.g. scaling)
Date:  2. October 2010, 11:42

This is the way it is designed.. You won't get a preview of what is happening while working within a prompt dialog. And since you're doing the scaling within onChange, the scaling happens even if you click on cancel.

If you want the cancel button to work you should check if the values variable contains something or not. Check line 44 onwards (I also cleaned up some of the code - check the way Point objects and Arrays are created.)

var settingsDialog;
var scaling = 0.0;

var shapes = [];

function onMouseDown() {
	createShapes();
	createSettingsDialog();
}

function createShapes() {
	if(shapes.length == 0) {
		var s1 = new Path.Circle(new Point(100, -100), 30);
			s1.fillColor = "#CCCCCC";
	
		var s2 = new Path.Circle(new Point(300, -500), 30);
			s2.fillColor = "#FF0000";
			
		var s3 = new Path.Circle(new Point(200, -300), 30);
			s3.fillColor = "#FF00FF";
			
			
		shapes.push(s1);
		shapes.push(s2);
		shapes.push(s3);
	}
}

function createSettingsDialog() {
	var enabled = true;
	
	settingsDialog = { 
		scaling: {
			type: "slider",
			label: "scaling size",
			value: scaling,
			range:[0, 50],
			enabled: enabled
		}
	}; 	
	
	var values = Dialog.prompt("settingsDialog", settingsDialog);
	// if the user clicked on OK:
	if(values) {
		scaleTriangles(scaling, true);
		scaleTriangles(values.scaling, false);
	}
}

function scaleTriangles(factor, rescale) {
	for(i in shapes) {
		shape = shapes[i];
			
		if(shape != null) {
			if(rescale) {
				scaling = 0.0;	
				shape.scale(1 / (1 + (factor / 100)));
			}
			else {
				scaling = factor;
				shape.scale( 1 + (factor / 100));	
			}		
		}
	}
	
}
Re: Problems with slider and function call (e.g. scaling)
From:  Axel
Date:  2. October 2010, 12:01

"You won't get a preview of what is happening while working within a prompt dialog."

Is there any other way of getting are sort of "live preview"?

Re: Problems with slider and function call (e.g. scaling)
Date:  2. October 2010, 12:07

You could use a Palette:

(I simplified your code a bit - check what is happening with the scaling..)

var lastScale = 1;

var values = {
	scaling: 1
}

var components = {
	scaling: {
		type: 'slider',
		label: 'scaling size',
		value: 1,
		range:[0.05, 4],
		enabled: true,
		onChange: function(value) {
			rescaleTriangles(value);
		}
	}
}

function rescaleTriangles(scale) {
	for(var i = 0; i < shapes.length; i++) {
		var shape = shapes[i];
		shape.scale((1 / lastScale) * scale);
	}
	lastScale = scale;
}

var s1 = new Path.Circle(new Point(100, -100), 30);
s1.fillColor = '#CCCCCC';

var s2 = new Path.Circle(new Point(300, -500), 30);
s2.fillColor = '#FF0000';

var s3 = new Path.Circle(new Point(200, -300), 30);
s3.fillColor = '#FF00FF';

var shapes = [s1, s2, s3];

var palette = new Palette('Scaler', components, values);
Re: Problems with slider and function call (e.g. scaling)
Date:  2. October 2010, 12:10

Oh yes, if you could explain how you're fitting this in within the workflow of the tool you're working on - we can advise you other ways to achieve what you want.

Scripts
08.08.14, 15:24
15.05.14, 14:23
02.03.14, 19:16
18.11.13, 14:48
22.03.13, 03:05
22.02.13, 15:45
Posts
10.01.17, 16:37
19.02.16, 06:03
19.02.16, 06:00
17.01.16, 11:00
12.01.16, 13:10
25.11.15, 08:19
Script of the Moment
Stroke Input 09.03.09