You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@wookie.apache.org by sc...@apache.org on 2010/02/12 11:06:07 UTC

svn commit: r909327 - in /incubator/wookie/trunk/widgets/sharedraw: ./ build.xml config.xml images/ images/icon.png index.html legal/ lib/ scripts/ scripts/draw.js scripts/sharedraw.js style/

Author: scottbw
Date: Fri Feb 12 10:06:07 2010
New Revision: 909327

URL: http://svn.apache.org/viewvc?rev=909327&view=rev
Log:
Added the "sharedraw" widget from the OSSWatch widget training day. This is a little collaborative drawing app that demonstrates how to use the HTML 5 Canvas feature with shared data.

Added:
    incubator/wookie/trunk/widgets/sharedraw/
    incubator/wookie/trunk/widgets/sharedraw/build.xml
    incubator/wookie/trunk/widgets/sharedraw/config.xml
    incubator/wookie/trunk/widgets/sharedraw/images/
    incubator/wookie/trunk/widgets/sharedraw/images/icon.png   (with props)
    incubator/wookie/trunk/widgets/sharedraw/index.html
    incubator/wookie/trunk/widgets/sharedraw/legal/
    incubator/wookie/trunk/widgets/sharedraw/lib/
    incubator/wookie/trunk/widgets/sharedraw/scripts/
    incubator/wookie/trunk/widgets/sharedraw/scripts/draw.js
    incubator/wookie/trunk/widgets/sharedraw/scripts/sharedraw.js
    incubator/wookie/trunk/widgets/sharedraw/style/

Added: incubator/wookie/trunk/widgets/sharedraw/build.xml
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/widgets/sharedraw/build.xml?rev=909327&view=auto
==============================================================================
--- incubator/wookie/trunk/widgets/sharedraw/build.xml (added)
+++ incubator/wookie/trunk/widgets/sharedraw/build.xml Fri Feb 12 10:06:07 2010
@@ -0,0 +1,23 @@
+<?xml version="1.0"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+<project default="build-widget" basedir="." name="widget build file">
+	<property name="wookie.widgets.dir" location="../"/>
+	<property name="widget.shortname" value="sharedraw"/>
+	
+	<import file="../build.xml"/>
+</project>
\ No newline at end of file

Added: incubator/wookie/trunk/widgets/sharedraw/config.xml
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/widgets/sharedraw/config.xml?rev=909327&view=auto
==============================================================================
--- incubator/wookie/trunk/widgets/sharedraw/config.xml (added)
+++ incubator/wookie/trunk/widgets/sharedraw/config.xml Fri Feb 12 10:06:07 2010
@@ -0,0 +1,47 @@
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+<widget xmlns="http://www.w3.org/ns/widgets"
+ 		id="http://wookie.apache.org/widgets/sharedraw"
+ 		version="0.1"
+        width="420"
+        height="320"
+        >
+  <feature name="http://wave.google.com" required="true"/>
+  <name>ShareDraw</name>
+  <description>A collaborative drawing application - sketch with friends!</description>
+  <content src="index.html"/>
+  <icon src="images/icon.png"/>
+  <author>Apache Wookie (Incubating) Team</author>
+  <licence>Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.</licence>
+
+</widget>
+
+
+

Added: incubator/wookie/trunk/widgets/sharedraw/images/icon.png
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/widgets/sharedraw/images/icon.png?rev=909327&view=auto
==============================================================================
Binary file - no diff available.

Propchange: incubator/wookie/trunk/widgets/sharedraw/images/icon.png
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: incubator/wookie/trunk/widgets/sharedraw/index.html
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/widgets/sharedraw/index.html?rev=909327&view=auto
==============================================================================
--- incubator/wookie/trunk/widgets/sharedraw/index.html (added)
+++ incubator/wookie/trunk/widgets/sharedraw/index.html Fri Feb 12 10:06:07 2010
@@ -0,0 +1,43 @@
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+<!DOCTYPE html>
+<html lang="en">
+  <head>
+    <meta charset="utf-8">
+    <title>Paint</title>
+    <style type="text/css"><!--
+      #container { position: relative; }
+      #imageView { border: 1px solid #000; cursor: crosshair }
+    --></style>
+  </head>
+  <body onload="Controller.init()">
+    <div id="container">
+      <canvas id="imageView" width="400" height="300">
+        <p>Unfortunately, your browser is currently unsupported by this widget.  
+        We are sorry for the inconvenience. Please use one of the 
+        supported browsers listed below:</p>
+        <p>Supported browsers: <a href="http://www.opera.com">Opera</a>, <a 
+          href="http://www.mozilla.com">Firefox</a>, <a 
+          href="http://www.apple.com/safari">Safari</a>, and <a 
+          href="http://www.konqueror.org">Konqueror</a>.</p>
+      </canvas>
+    </div>
+
+    <script type="text/javascript" src="scripts/sharedraw.js"></script>
+    <script type="text/javascript" src="scripts/draw.js"></script>
+  </body>
+</html>
\ No newline at end of file

Added: incubator/wookie/trunk/widgets/sharedraw/scripts/draw.js
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/widgets/sharedraw/scripts/draw.js?rev=909327&view=auto
==============================================================================
--- incubator/wookie/trunk/widgets/sharedraw/scripts/draw.js (added)
+++ incubator/wookie/trunk/widgets/sharedraw/scripts/draw.js Fri Feb 12 10:06:07 2010
@@ -0,0 +1,71 @@
+var canvas, context, tool;
+  
+function init_canvas () {
+    // Find the canvas element.
+    canvas = document.getElementById('imageView');
+    if (!canvas) {
+      alert('Error: I cannot find the canvas element!');
+      return;
+    }
+
+    if (!canvas.getContext) {
+      alert('Error: no canvas.getContext!');
+      return;
+    }
+
+    // Get the 2D canvas context.
+    context = canvas.getContext('2d');
+    if (!context) {
+      alert('Error: failed to getContext!');
+      return;
+    }
+
+    // Pencil tool instance.
+    tool = new tool_pencil();
+
+    // Attach the mousedown, mousemove and mouseup event listeners.
+    canvas.addEventListener('mousedown', ev_canvas, false);
+    canvas.addEventListener('mousemove', ev_canvas, false);
+    canvas.addEventListener('mouseup',   ev_canvas, false);
+  }
+
+  function tool_pencil () {
+    var tool = this;
+    this.started = false;
+    this.mousedown = function (ev) {
+        tool.line = new Array();
+        context.beginPath();
+        context.moveTo(ev._x, ev._y);
+        tool.line[0] = ev._x + ":" + ev._y;
+        tool.started = true;
+    };
+    this.mousemove = function (ev) {
+      if (tool.started) {
+        tool.line.push(ev._x + ":" + ev._y);
+        context.lineTo(ev._x, ev._y);
+        context.stroke();
+      }
+    };
+    this.mouseup = function (ev) {
+      if (tool.started) {
+        tool.mousemove(ev);
+        tool.started = false;
+        // Save the action
+        line = new Line(Line.guid(),tool.line,null);
+        line.save();
+      }
+    };
+  }
+  function ev_canvas (ev) {
+    if (ev.layerX || ev.layerX == 0) { // Firefox
+      ev._x = ev.layerX;
+      ev._y = ev.layerY;
+    } else if (ev.offsetX || ev.offsetX == 0) { // Opera
+      ev._x = ev.offsetX;
+      ev._y = ev.offsetY;
+    }
+    var func = tool[ev.type];
+    if (func) {
+      func(ev);
+    }
+  }
\ No newline at end of file

Added: incubator/wookie/trunk/widgets/sharedraw/scripts/sharedraw.js
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/widgets/sharedraw/scripts/sharedraw.js?rev=909327&view=auto
==============================================================================
--- incubator/wookie/trunk/widgets/sharedraw/scripts/sharedraw.js (added)
+++ incubator/wookie/trunk/widgets/sharedraw/scripts/sharedraw.js Fri Feb 12 10:06:07 2010
@@ -0,0 +1,106 @@
+/**
+ * The Line class
+ */ 
+function Line(id,path,user){
+    this.id = id;
+    this.path = path;
+    this.user = user;
+}
+
+//// Instance methods
+
+// Save
+Line.prototype.save = function(){
+	if (typeof(wave) != "undefined") wave.getState().submitValue(this.id, JSON.stringify(this));
+}
+
+//// Static methods
+
+Line.create = function(json){
+		var obj = JSON.parse(json);
+        var line = new Line(obj.id, obj.path,obj.user);
+        return line;
+}
+
+// Find
+Line.find = function(id){
+        var keys = wave.getState().getKeys();
+        for (var i = 0; i < keys.length; i++) {
+            var key = keys[i];
+            if (key == task_id){
+                return Line.create(wave.getState().get(key));
+            }
+        }
+        return null;
+}
+
+// Find all
+Line.findAll = function(){
+    var lines = {};
+	if (typeof(wave) != "undefined"){
+        var keys = wave.getState().getKeys();
+        for (var i = 0; i < keys.length; i++) {
+        	var key = keys[i];
+        	var line = Line.create(wave.getState().get(key));
+        	lines[key] = line;
+        }
+    }
+    return lines;
+    
+}
+
+// Make a guid for a new object
+Line.guid = function(){
+    var uid = function() {return (((1+Math.random())*0x10000)|0).toString(16).substring(1);}
+    return uid()+uid()+"-"+uid()+"-"+uid()+"-"+uid()+"-"+uid()+uid()+uid();
+}
+
+function rnd_no(max){
+    return Math.floor(Math.random()*max);
+};
+
+/**
+ * The Controller object
+ * This is used to wire up the view and model with actions
+ */ 
+var Controller = {
+
+    // Event handler
+    // Update the view when state has been updated
+    stateUpdated: function(){
+        var lines = Line.findAll();
+        if (lines && lines != null){
+            for (key in lines) {
+                var line = lines[key]; 
+                var start = line.path[0].split(":");
+                context.beginPath();
+                context.moveTo(start[0],start[1]);
+                for (point in line.path){
+                    var coords = line.path[point].split(":");
+                    context.lineTo(coords[0],coords[1]);
+                    context.stroke();
+                    //context.moveTo(coords[0],coords[1]);
+                }
+                context.closePath();
+                
+                   // context.fillRect  (start[0],   start[1], start[0]+150, 150);
+             }
+        }
+    },
+    
+    participantsUpdated: function(){
+    },
+    
+    init: function(){
+        init_canvas();
+    	Controller.participantsUpdated();
+    	Controller.stateUpdated();
+    	/**
+    	 * Register the event handlers with the Wave feature
+    	 */
+    	if (typeof(wave) != "undefined"){
+    		wave.setStateCallback(Controller.stateUpdated);
+    		wave.setParticipantCallback(Controller.participantsUpdated);
+    	}
+    }
+}
\ No newline at end of file