|
private void drawDashedLine(JavaScriptObject
context, float fromX, float fromY, float toX, float toY,
JsArrayInteger pattern)
{
beginPath(context);
Boolean xgreaterThan = true;
Boolean ygreaterThan = true;
if (fromY - toY > 0)
ygreaterThan = false;
if (fromX - toX > 0)
xgreaterThan = false;
MoveTo(context, fromX, fromY);
float offsetX =
fromX;
float offsetY =
fromY;
int idx = 0;
Boolean dash = true;
float ang = (float) Math.atan2(toY
- fromY, toX - fromX);
float cosAng = (float) Math.cos(ang);
float sinAng = (float) Math.sin(ang);
while
(!(thereYet(xgreaterThan, offsetX, toX) && thereYet(ygreaterThan,
offsetY, toY))) {
int len =
pattern.get(idx);
offsetX = cap(xgreaterThan, toX, offsetX +
(cosAng * len));
offsetY = cap(ygreaterThan, toY, offsetY +
(sinAng * len));
if (dash)
LineTo(context, offsetX, offsetY);
else
MoveTo(context, offsetX, offsetY);
idx = (idx + 1) % pattern.length();
dash = !dash;
}
endPath(context);
}
private Boolean thereYet(Boolean greaterThan, float a, float b)
{
if (greaterThan)
return a >= b;
else
return a <= b;
}
private float cap(Boolean
greaterThan, float a, float b)
{
if (greaterThan)
return Math.min(a,
b);
else
return Math.max(a,
b);
}
private final native void
beginPath(JavaScriptObject context) /*-{
context.beginPath();
}-*/;
private final native void
endPath(JavaScriptObject context) /*-{
context.stroke();
context.closePath();
}-*/;
|