screentoworld misunderstanding

Nov 7, 2007 at 6:18pm

screentoworld misunderstanding

Hi.
I am trying to write a very basic jsui slider, just by modifying the simpleslider.js from the examples.
Everything went ok until I tried to make the slider horizontal.
It looks like I have a misunderstanding of “sketch.screentoworld”.

In vertical mode, I have this working perfectly:
——————————————–
function ondrag(x,y,….)
{
var f,a;
a=sketch.screentoworld(x,y);
f=(a[1]+1)*0.5;
msg_float(f);
}
——————————————–
I thought that replacing a[1] with a[0] would be enough to deal with x coordinates, but for example, if my object is 100×10 pixels, dragging will work horizontally only within 10 pixels around the center (5 left and 5 right) and not on the whole 100…

I had a look at other jsui sliders (matsui,…) but they are way too complicated for me…

Any help is welcome.
Thanx.

#34507
Nov 7, 2007 at 10:07pm

On 7 nov. 07, at 19:18, gusanomaxlist wrote:

> In vertical mode, I have this working perfectly:
> ——————————————–
> function ondrag(x,y,….)
> {
> var f,a;
> a=sketch.screentoworld(x,y);
> f=(a[1]+1)*0.5;
> msg_float(f);
> }
> ——————————————–
> I thought that replacing a[1] with a[0] would be enough to deal
> with x coordinates, but for example, if my object is 100×10 pixels,
> dragging will work horizontally only within 10 pixels around the
> center (5 left and 5 right) and not on the whole 100…

Your problem is that on the y axis the value are from -1 to 1 (with 0
being the center of the vertical axis). But for the x axis the
location is from -aspect to +aspect (where aspect is the box width/
box height). Have a look to the “jsui_screentoworld-example.pat”
example in the examples/javascript/ui folder.

something like that should work, although I didn’t test it:

function ondrag(x,y,….)
{
var width = box.rect[2] – box.rect[0];
var height = box.rect[3] – box.rect[1];
var aspect = width/height;

var a = sketch.screentoworld(x,y);
var f = (a[0] + aspect) * aspect * 0.5;
msg_float(f);
}

HTH,
ej

#116648
Nov 8, 2007 at 10:28am

got it ;)

f = (a[0]+aspect)/aspect*0.5;

Thanx a lot !

#116649
Nov 8, 2007 at 12:02pm

#116650
Nov 9, 2007 at 6:35pm

Hallo.
I’m bothering you again…
Now I’m stucked on the fine tuning (with shift key).
It’s (nearly!!) working, but if I press shift while dragging, I loose my value and the slider is fine tuning but re-starting from zero-left.
Thanx for your hints.
Here is the code:

———————————————
function ondrag(x,y,…)

var f,a,v,dx;
a = sketch.screentoworld(x,y);
v = (a[0]+aspect)/aspect*0.5;
dx = v – last_x*0.2; //(last_x is initiated with 0)

if (shift)
{
f = v – dx;
} else {
f = v;
}
msg_float(f);
last_x = v;
}
———————————————

#116651
Nov 9, 2007 at 7:56pm

On 9 nov. 07, at 19:35, gusanomaxlist wrote:

> Thanx for your hints.
> Here is the code:

Something like that?

function ondrag(x,y,…)
{
var f,a,v,dx;
a = sketch.screentoworld(x,y);
v = (a[0]+aspect)/aspect*0.5;
dx = v – last_x; //(last_x is initiated with 0)

if (shift)
dx *= 0.2;

f = v – dx;

msg_float(f);
last_x = v;
}

ej

#116652
Nov 9, 2007 at 10:12pm

Thanx Emmanuel, but I still don’t get it.
With this:

if (shift) {
dx = 0.2;
f = v – dx; …

I just have a left offset from my mouse, and no “fine tuning”.
Any idea ?
Cheers.

#116653
Nov 9, 2007 at 10:19pm

On 9 nov. 07, at 23:12, gusanomaxlist wrote:

> Thanx Emmanuel, but I still don’t get it.
> With this:
>
> if (shift) {
> dx = 0.2;
> f = v – dx; …
>
> I just have a left offset from my mouse, and no “fine tuning”.
> Any idea ?

I suggested that:

dx = v – last_x; //(last_x is initiated with 0)

// if shift is down, reduce the dx to have “fine tuning
if (shift)
dx *= 0.2; // means dx = dx * 0.2

f = v – dx;

If that doesn’t work, please provide the all jsui.
ej

#116654
Nov 9, 2007 at 11:20pm

sorry, I miswrote it.
I am sure that it’s something really stupid but I just can’t find it.
The js is attached.
Thanx.

#116655

You must be logged in to reply to this topic.