summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorxant <xant@dyne.org>2011-03-27 15:54:52 (GMT)
committer xant <xant@dyne.org>2011-03-27 15:54:52 (GMT)
commitc0a8625be7e82c654f8f7dc6b5cc6e9ea90095cc (patch)
tree6fc0dca3a70250871d287d229a2c132b8940dff5
parentdc7856e9e5891fa92014038a84e6167a62ac1028 (diff)
drawing paths through the canvas-element API now workscanvas
exposing frand() in the javascript context as helper function to generate a random float between 0.0 and 1.0
-rw-r--r--core/JMXDrawPath.h2
-rw-r--r--core/JMXDrawPath.mm50
-rw-r--r--core/JMXScript.mm8
-rw-r--r--javascript_examples/test_canvas.js6
-rw-r--r--javascript_examples/test_drawing.js8
5 files changed, 58 insertions, 16 deletions
diff --git a/core/JMXDrawPath.h b/core/JMXDrawPath.h
index 9d48f99..d26bf5b 100644
--- a/core/JMXDrawPath.h
+++ b/core/JMXDrawPath.h
@@ -37,7 +37,7 @@
@private
JMXSize *_frameSize;
BOOL _clear;
- NSMutableArray *subPaths;
+ NSUInteger subPaths;
}
/*!
diff --git a/core/JMXDrawPath.mm b/core/JMXDrawPath.mm
index 74b30d9..a48f62b 100644
--- a/core/JMXDrawPath.mm
+++ b/core/JMXDrawPath.mm
@@ -162,7 +162,7 @@ using namespace v8;
} else if (gradient.mode = kJMXCanvasGradientRadial) {
}
}*/
- CGContextStrokePath(context);
+ CGContextDrawPath(context, kCGPathFillStroke);
CGContextFillPath(context);
CGContextRestoreGState(context);
[lock unlock];
@@ -455,7 +455,9 @@ using namespace v8;
{
[lock lock];
UInt32 pathIndex = pathLayerOffset%kJMXDrawPathBufferCount;
+ //CGContextSaveGState(CGLayerGetContext(pathLayers[pathIndex]));
CGContextBeginPath(CGLayerGetContext(pathLayers[pathIndex]));
+ subPaths++;
[lock unlock];
}
@@ -464,6 +466,8 @@ using namespace v8;
[lock lock];
UInt32 pathIndex = pathLayerOffset%kJMXDrawPathBufferCount;
CGContextClosePath(CGLayerGetContext(pathLayers[pathIndex]));
+ //CGContextRestoreGState(CGLayerGetContext(pathLayers[pathIndex]));
+ subPaths--;
[lock unlock];
}
@@ -511,7 +515,24 @@ using namespace v8;
{
[lock lock];
UInt32 pathIndex = pathLayerOffset%kJMXDrawPathBufferCount;
- CGContextFillPath(CGLayerGetContext(pathLayers[pathIndex]));
+ CGContextRef context = CGLayerGetContext(pathLayers[pathIndex]);
+ if ([fillStyle isKindOfClass:[NSColor class]]) {
+ CGContextSetRGBFillColor (context,
+ [(NSColor *)fillStyle redComponent],
+ [(NSColor *)fillStyle greenComponent],
+ [(NSColor *)fillStyle blueComponent],
+ [(NSColor *)fillStyle alphaComponent]
+ );
+ } else if ([fillStyle isKindOfClass:[JMXCanvasPattern class]]) {
+ CGContextSetFillPattern(context, [(JMXCanvasPattern *)fillStyle patternRef], [(JMXCanvasPattern *)fillStyle components]);
+ }/* else if ([fillColor isKindOfClass:[JMXCanvasGradient class]]) {
+ JMXCanvasGradient *gradient = (JMXCanvasGradient *)fillColor;
+ if (gradient.mode = kJMXCanvasGradientLinear) {
+ CGContextDrawLinearGradient(context, [(JMXCanvasGradient *)strokeColor gradientRef], <#CGPoint startPoint#>, <#CGPoint endPoint#>, <#CGGradientDrawingOptions options#>)
+ } else if (gradient.mode = kJMXCanvasGradientRadial) {
+ }
+ }*/
+ CGContextFillPath(context);
[lock unlock];
}
@@ -519,7 +540,20 @@ using namespace v8;
{
[lock lock];
UInt32 pathIndex = pathLayerOffset%kJMXDrawPathBufferCount;
- CGContextStrokePath(CGLayerGetContext(pathLayers[pathIndex]));
+ CGContextRef context = CGLayerGetContext(pathLayers[pathIndex]);
+ if ([strokeStyle isKindOfClass:[NSColor class]]) {
+ CGContextSetRGBStrokeColor (context,
+ [(NSColor *)strokeStyle redComponent],
+ [(NSColor *)strokeStyle greenComponent],
+ [(NSColor *)strokeStyle blueComponent],
+ [(NSColor *)strokeStyle alphaComponent]
+ );
+ } else if ([strokeStyle isKindOfClass:[JMXCanvasPattern class]]) {
+ CGContextSetStrokePattern(context, [(JMXCanvasPattern *)strokeStyle patternRef], [(JMXCanvasPattern *)strokeStyle components]);
+ }
+
+
+ CGContextDrawPath(context, kCGPathFillStroke);
[lock unlock];
}
@@ -587,13 +621,15 @@ using namespace v8;
[lock lock];
UInt32 pathIndex = pathLayerOffset%kJMXDrawPathBufferCount;
CGContextRef context = CGLayerGetContext(pathLayers[pathIndex]);
- CGContextDrawPath(context, kCGPathFillStroke);
- CGContextFillPath(context);
+ if (!subPaths) {
+ CGContextDrawPath(context, kCGPathFillStroke);
+ CGContextFillPath(context);
+ }
[lock unlock];
if (currentFrame)
[currentFrame release];
- currentFrame = [[CIImage imageWithCGLayer:pathLayers[pathIndex]]retain];
+ currentFrame = [[CIImage imageWithCGLayer:pathLayers[pathIndex]] retain];
}
- (CIImage *)currentFrame
@@ -785,7 +821,7 @@ static v8::Handle<Value> ClosePath(const Arguments& args)
//v8::Locker lock;
HandleScope handleScope;
JMXDrawPath *drawPath = (JMXDrawPath *)args.Holder()->GetPointerFromInternalField(0);
- [drawPath beginPath];
+ [drawPath closePath];
return Undefined();
}
diff --git a/core/JMXScript.mm b/core/JMXScript.mm
index 293f99b..af2c030 100644
--- a/core/JMXScript.mm
+++ b/core/JMXScript.mm
@@ -194,6 +194,13 @@ static v8::Handle<Value> Rand(const Arguments& args) {
return scope.Close(v8::Integer::New(rand()));
}
+
+static v8::Handle<Value> FRand(const Arguments& args) {
+ //v8::Locker lock;
+ HandleScope scope;
+ return scope.Close(v8::Number::New(rand() / (RAND_MAX + 1.0)));
+}
+
static v8::Handle<Value> Echo(const Arguments& args) {
if (args.Length() < 1) return v8::Undefined();
//v8::Locker lock;
@@ -424,6 +431,7 @@ static v8::Handle<Value> GetDocument(v8::Local<v8::String> name, const v8::Acces
Local<ObjectTemplate>ctxTemplate = ObjectTemplate::New();
ctxTemplate->Set(String::New("rand"), FunctionTemplate::New(Rand));
+ ctxTemplate->Set(String::New("frand"), FunctionTemplate::New(FRand));
ctxTemplate->Set(String::New("echo"), FunctionTemplate::New(Echo));
ctxTemplate->Set(String::New("print"), FunctionTemplate::New(Echo));
ctxTemplate->Set(String::New("include"), FunctionTemplate::New(Include));
diff --git a/javascript_examples/test_canvas.js b/javascript_examples/test_canvas.js
index f167960..668fe03 100644
--- a/javascript_examples/test_canvas.js
+++ b/javascript_examples/test_canvas.js
@@ -13,9 +13,11 @@ while (1) {
k = rand();
p.x = rand()%drawer.size.width;
p.y = rand()%drawer.size.height;
- c = new Color(1.0, 0.0, 1.0);
+ c = new Color(frand(), frand(), frand(), 0.5);
+ b = new Color(frand(), frand(), frand(), 0.5);
drawer.canvas.strokeText("TEST", p, null, c);
- drawer.canvas.strokeStyle = new Color(rand()%1000/1000, rand()%1000/1000, rand()%1000/1000);
+ drawer.canvas.strokeStyle = new Color(frand(), frand(), frand());
+ drawer.canvas.fillStyle = b;
drawer.canvas.arc(rand()%drawer.size.width, rand()%drawer.size.height, rand()%drawer.size.height, 0, 360, 0);
sleep(1/60);
}
diff --git a/javascript_examples/test_drawing.js b/javascript_examples/test_drawing.js
index 2f5b09f..43559b6 100644
--- a/javascript_examples/test_drawing.js
+++ b/javascript_examples/test_drawing.js
@@ -19,17 +19,13 @@ x = 0;
y = 0;
radius = 20;
-function randFloat() {
- return rand()%1000/1000;
-}
-
echo(dumpDOM());
mainloop = function() {
//drawer.clear();
point = new Point(rand()%width/2, rand()%height/2);
- fgColor = new Color(randFloat(), randFloat(), randFloat(), randFloat());
- bgColor = new Color(randFloat(), randFloat(), randFloat(), randFloat());
+ fgColor = new Color(frand(), frand(), frand(), frand());
+ bgColor = new Color(frand(), frand(), frand(), frand());
radius = rand()%width/2;
point1 = new Point(rand()%width, rand()%height);
point2 = new Point(rand()%width, rand()%height);