Changeset 65

Show
Ignore:
Timestamp:
04/20/07 20:16:55 (21 months ago)
Author:
steadicat
Message:

Some profiling and refactoring.
Feature: corners.

Location:
hacks/trunk/canvas
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • hacks/trunk/canvas/default.js

    r64 r65  
    77 
    88function mouseDown(event) { 
    9     createPoint(event.clientX, event.clientY); 
     9    var point = new Point(event.clientX, event.clientY); 
    1010} 
    1111 
    1212function mouseMove(event) { 
    13     if (editing) dragDot(editing, event.clientX, event.clientY); 
     13    if (editing) moveDot(editing, event.clientX, event.clientY); 
    1414} 
    1515 
     
    1818} 
    1919 
     20 
     21function Point(x, y) { 
     22 
     23    var point = dot(x, y, 10); 
     24    point.g = document.createElementNS("http://www.w3.org/2000/svg", "g"); 
     25    addClass(point.g, "point"); 
     26 
     27    point.handle1 = createHandle(point); 
     28    point.handle2 = createHandle(point); 
     29 
     30    point.handle1.linkedHandle = point.handle2; 
     31    point.handle2.linkedHandle = point.handle1; 
     32 
     33    editing = point.handle2; 
     34 
     35    if (!curve) { 
     36        startCurve(point); 
     37    } else { 
     38        extendCurve(point); 
     39    } 
     40 
     41    $(point.g).append(point); 
     42    $(curve.g).append(point.g); 
     43} 
    2044 
    2145function addClass(element, c) { 
     
    5377            editing = dot; 
    5478            addClass(dot, "editing"); 
     79 
     80            // alt unlinks two handles 
     81            if (event.altKey && dot.linkedHandle) { 
     82                dot.linkedHandle.linkedHandle = null; 
     83                dot.linkedHandle = null; 
     84            } 
    5585        }); 
    5686    $(dot).mouseup(function(event) { 
     
    105135    handle.line = document.createElementNS("http://www.w3.org/2000/svg", "path"); 
    106136 
    107     handle.line.pathSegList.appendItem(handle.line.createSVGPathSegMovetoAbs(handle.cx.baseVal.value, handle.cy.baseVal.value)); 
    108     handle.line.pathSegList.appendItem(handle.line.createSVGPathSegLinetoAbs(point.cx.baseVal.value, point.cy.baseVal.value)); 
     137    handle.moveTo = handle.line.createSVGPathSegMovetoAbs(handle.cx.baseVal.value, handle.cy.baseVal.value); 
     138    handle.lineTo = handle.line.createSVGPathSegLinetoAbs(point.cx.baseVal.value, point.cy.baseVal.value); 
     139    handle.line.pathSegList.appendItem(handle.moveTo); 
     140    handle.line.pathSegList.appendItem(handle.lineTo); 
    109141    $(point.g).append(handle.line); 
    110142    $(point.g).append(handle); 
     
    112144} 
    113145 
    114 function createPoint(x, y) { 
    115  
    116     var point = dot(x, y, 10); 
    117     point.g = document.createElementNS("http://www.w3.org/2000/svg", "g"); 
    118     addClass(point.g, "point"); 
    119  
    120     point.handle1 = createHandle(point); 
    121     point.handle2 = createHandle(point); 
    122  
    123     point.handle1.linkedHandle = point.handle2; 
    124     point.handle2.linkedHandle = point.handle1; 
    125  
    126     editing = point.handle2; 
    127  
    128     if (!curve) { 
    129         startCurve(point); 
     146 
     147 
     148function moveDot(dot, x, y) { 
     149    if (dot.line) { 
     150        moveHandle(dot, x, y); 
    130151    } else { 
    131         extendCurve(point); 
    132     } 
    133  
    134     $(point.g).append(point); 
    135     $(curve.g).append(point.g); 
    136 } 
    137  
    138  
    139 function moveDot(dot, x, y) { 
    140  
    141     // move the dot itself 
    142     dot.cx.baseVal.value = x; 
    143     dot.cy.baseVal.value = y; 
    144  
    145     // move associated lines 
    146     if (dot.line) { 
    147         dot.line.pathSegList.getItem(0).x = x; 
    148         dot.line.pathSegList.getItem(0).y = y; 
    149         dot.line.pathSegList.getItem(1).x = dot.point.cx.baseVal.value; 
    150         dot.line.pathSegList.getItem(1).y = dot.point.cy.baseVal.value; 
    151     } 
    152  
    153     if (dot.leading) { 
    154         dot.leading.curveTo.x2 = x; 
    155         dot.leading.curveTo.y2 = y; 
    156     } 
    157     if (dot.trailing) { 
    158         dot.trailing.curveTo.x1 = x; 
    159         dot.trailing.curveTo.y1 = y; 
    160     } 
    161 } 
    162  
    163 function dragDot(dot, x, y) { 
    164  
    165     var dx = x - dot.cx.baseVal.value; 
    166     var dy = y - dot.cy.baseVal.value; 
    167  
    168     moveDot(dot, x, y); 
     152        movePoint(dot, x, y); 
     153    } 
     154} 
     155 
     156function movePoint(point, x, y) { 
     157    var dx = x - point.cx.baseVal.value; 
     158    var dy = y - point.cy.baseVal.value; 
     159 
     160    // move the point itself 
     161    point.cx.baseVal.value = x; 
     162    point.cy.baseVal.value = y; 
    169163 
    170164    // move associated handles 
    171     if (dot.handle1) { 
    172         var hx = dot.handle1.cx.baseVal.value+dx; 
    173         var hy = dot.handle1.cy.baseVal.value+dy; 
    174         moveDot(dot.handle1, hx, hy); 
    175     } 
    176     if (dot.handle2) { 
    177         var hx = dot.handle2.cx.baseVal.value+dx; 
    178         var hy = dot.handle2.cy.baseVal.value+dy; 
    179         moveDot(dot.handle2, hx, hy); 
    180     } 
    181     if (dot.linkedHandle) { 
    182         var hx = dot.linkedHandle.cx.baseVal.value-dx; 
    183         var hy = dot.linkedHandle.cy.baseVal.value-dy; 
    184         moveDot(dot.linkedHandle, hx, hy); 
     165    if (point.handle1) { 
     166        var hx = point.handle1.cx.baseVal.value+dx; 
     167        var hy = point.handle1.cy.baseVal.value+dy; 
     168        moveHandle(point.handle1, hx, hy, true); 
     169        var hx = point.handle2.cx.baseVal.value+dx; 
     170        var hy = point.handle2.cy.baseVal.value+dy; 
     171        moveHandle(point.handle2, hx, hy, true); 
    185172    } 
    186173 
    187174    // move curve 
    188     if (dot.curveTo) { 
    189         dot.curveTo.x = x; 
    190         dot.curveTo.y = y; 
    191     } 
    192 } 
     175    if (point.curveTo) { 
     176        point.curveTo.x = x; 
     177        point.curveTo.y = y; 
     178    } 
     179} 
     180 
     181function moveHandle(handle, x, y, stop) { 
     182 
     183    // move linked handle 
     184    if ((!stop) && handle.linkedHandle) { 
     185        var hx = handle.linkedHandle.cx.baseVal.value- (x-handle.cx.baseVal.value); 
     186        var hy = handle.linkedHandle.cy.baseVal.value- (y-handle.cy.baseVal.value); 
     187        moveHandle(handle.linkedHandle, hx, hy, true); 
     188    } 
     189 
     190    // move the handle itself 
     191    handle.cx.baseVal.value = x; 
     192    handle.cy.baseVal.value = y; 
     193 
     194    // move associated line 
     195    handle.moveTo.x = x; 
     196    handle.moveTo.y = y; 
     197    handle.lineTo.x = handle.point.cx.baseVal.value; 
     198    handle.lineTo.y = handle.point.cy.baseVal.value; 
     199 
     200    // move curve 
     201    if (handle.leading) { 
     202        handle.leading.curveTo.x2 = x; 
     203        handle.leading.curveTo.y2 = y; 
     204    } else if (handle.trailing) { 
     205        handle.trailing.curveTo.x1 = x; 
     206        handle.trailing.curveTo.y1 = y; 
     207    } 
     208} 
     209 
    193210 
    194211// set up 
     
    202219        }); 
    203220    }); 
    204 }); 
    205  
     221 
     222 
  • hacks/trunk/canvas/index.xhtml

    r64 r65  
    2020  <body class="main"> 
    2121 
    22     <p class="instructions">Enter to close a path, Esc to interrupt it.</p> 
     22    <p class="instructions">Enter to close a path, Esc to interrupt 
     23    it. Hold down Alt while dragging a handle to create a corner.</p> 
    2324 
    2425    <svg xmlns="http://www.w3.org/2000/svg"