Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: drpancake/JPCanvasViewController
base: 54aabf22ee
...
head fork: drpancake/JPCanvasViewController
compare: 9802853575
  • 5 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
View
13 JPCanvasViewController.h
@@ -3,6 +3,7 @@
// Created by James Potter on 05/03/2012.
//
+#import "JPCanvasViewControllerDelegate.h"
#import "SBJson.h"
/*
@@ -19,8 +20,13 @@
WebView *_webView;
}
+- (void)didDrawPoints:(WebScriptObject *)scriptObject;
+
@property (nonatomic, strong, readonly) WebView *webView;
+// TODO: fix runtime error if this is set to a weak reference (ARC bug?)
+@property (nonatomic, strong) id<JPCanvasViewControllerDelegate> delegate;
+
/*
iOS
*/
@@ -36,13 +42,18 @@
}
@property (nonatomic, strong, readonly) UIWebView *webView;
+@property (nonatomic, weak) id<JPCanvasViewControllerDelegate> delegate;
#endif
/*
- Shared methods
+ Shared
*/
+// TODO: this should be shared
+//@property (nonatomic, weak) id<JPCanvasViewControllerDelegate> delegate;
+
- (NSString *)execute:(NSString *)js;
+- (void)drawPoints:(NSArray *)points;
@end
View
45 JPCanvasViewController.m
@@ -8,6 +8,7 @@
@implementation JPCanvasViewController
@synthesize webView=_webView;
+@synthesize delegate;
/*
OS X specific methods for preparing a WebView
@@ -25,9 +26,6 @@ - (WebView *)webView
- (void)loadView
{
- // Load nib
- [super loadView];
-
[self.webView setFrameLoadDelegate:self];
WebFrame *webFrame = [self.webView mainFrame];
@@ -36,13 +34,43 @@ - (void)loadView
[self setView:self.webView];
}
-// WebFrameLoadDelegate
+// WebFrameLoadDelegate method
+- (void)webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)windowObject forFrame:(WebFrame *)frame
+{
+ // Expose this JPCanvasViewController instance to JS
+ WebScriptObject *window = [self.webView windowScriptObject];
+ [window setValue:self forKey:@"controller"];
+}
+
+// WebFrameLoadDelegate method
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
NSString *js = @"window.context.fillStyle='red'; window.context.fillRect(50, 50, 100, 100);";
[self execute:js];
}
+// Tell WebScriptObject which methods to expose to JS
++ (BOOL)isSelectorExcludedFromWebScript:(SEL)selector
+{
+ if (selector == @selector(didDrawPoints:)) {
+ return NO;
+ }
+ return YES;
+}
+
+- (void)didDrawPoints:(WebScriptObject *)scriptObject
+{
+ int length = [(NSNumber *)[scriptObject valueForKey:@"length"] intValue];
+ NSMutableArray *points = [[NSMutableArray alloc] initWithCapacity:length];
+ WebScriptObject *pointsObject = [scriptObject valueForKey:@"points"];
+ for (int i=0; i < length; i++) {
+ [points addObject:[pointsObject webScriptValueAtIndex:i]];
+ }
+
+ NSLog(@"didDrawPoints got %lu points", [points count]);
+ [self.delegate canvas:self didDrawPoints:points];
+}
+
/*
iOS-specific methods for preparing a UIWebView
*/
@@ -94,7 +122,8 @@ - (BOOL)webView:(UIWebView *)theWebView
// TODO: handler method
if ([function isEqualToString:@"didDrawPoints"]) {
- NSLog(@"Got points: %@", args);
+ NSLog(@"JPCanvas got points (len = %i)", [args count]);
+ [self.delegate canvas:self didDrawPoints:args];
}
return NO;
@@ -120,4 +149,10 @@ - (NSString *)execute:(NSString *)js
return [self.webView stringByEvaluatingJavaScriptFromString:js];
}
+- (void)drawPoints:(NSArray *)points
+{
+ NSString *js = [NSString stringWithFormat:@"drawPoints([%@]);", [points componentsJoinedByString:@","]];
+ [self execute:js];
+}
+
@end
View
17 JPCanvasViewControllerDelegate.h
@@ -0,0 +1,17 @@
+//
+// JPCanvasViewControllerDelegate.h
+// CubicusClient
+//
+// Created by James Potter on 13/03/2012.
+// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+@class JPCanvasViewController;
+
+@protocol JPCanvasViewControllerDelegate <NSObject>
+
+- (void)canvas:(JPCanvasViewController *)canvas didDrawPoints:(NSArray *)points;
+
+@end
View
39 canvas.html
@@ -30,7 +30,7 @@
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var buttonDown = false;
-var currentPoints;
+var currentPoints; // Flat list: [x1, y1, x2, y2, ...]
// Detect iOS
@@ -74,7 +74,9 @@
buttonDown = true;
// Start recording this path
- currentPoints = [pos];
+ currentPoints = [];
+ currentPoints.push(pos.x);
+ currentPoints.push(pos.y);
}
e.preventDefault();
}
@@ -84,15 +86,23 @@
var pos = getPosition(e);
context.lineTo(pos.x, pos.y);
context.stroke();
-
- currentPoints.push(pos);
+
+ // Add to the path
+ currentPoints.push(pos.x);
+ currentPoints.push(pos.y);
}
}
function stopPaint() {
buttonDown = false;
-
- NativeBridge.call("didDrawPoints", currentPoints);
+
+ if (iOSDetected) {
+ NativeBridge.call("didDrawPoints", currentPoints);
+ } else {
+ // WebKit type conversions require us to specify the array's length
+ window.controller.didDrawPoints_({length: currentPoints.length, points: currentPoints});
+ }
+
currentPoints = null;
}
@@ -107,6 +117,23 @@
canvas.addEventListener('mousemove', continuePaint, false);
canvas.addEventListener('mouseup', stopPaint, false);
}
+
+// Called externally from Obj-C to interact with the canvas programmatically
+
+function drawPoints(points) {
+ context.beginPath();
+
+ if (points.length >= 2) {
+ context.moveTo(points[0], points[1]);
+ }
+
+ if (points.length > 2) {
+ for (i=2; i < points.length; i += 2) {
+ context.lineTo(points[i], points[i+1]);
+ }
+ }
+ context.stroke();
+}
</script>
</body>

No commit comments for this range

Something went wrong with that request. Please try again.