Changeset 24
- Timestamp:
- 02/05/06 11:18:05 (2 years ago)
- Files:
-
- plotkit/trunk/PlotKit/excanvas.js (modified) (17 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plotkit/trunk/PlotKit/excanvas.js
r18 r24 16 16 // TODO: Radial gradient 17 17 // TODO: Clipping paths 18 // TODO: Coordsize 18 // TODO: Coordsize (still need to support stretching) 19 19 // TODO: Painting mode 20 20 // TODO: Optimize 21 21 // TODO: canvas width/height sets content size in moz, border size in ie 22 // TODO: Painting outside the canvas should not be allowed23 22 24 23 // only add this code if we do not already have a canvas implementation … … 26 25 27 26 (function () { 27 28 // alias some functions to make (compiled) code shorter 29 var m = Math; 30 var mr = m.round; 31 var ms = m.sin; 32 var mc = m.cos; 28 33 29 34 var G_vmlCanvasManager_ = { … … 87 92 * Public initializes a canvas element so that it can be used as canvas 88 93 * element from now on. This is called automatically before the page is 89 * loaded but if you are creating elements using createElement y uoneed to94 * loaded but if you are creating elements using createElement you need to 90 95 * make sure this is called on the element. 91 * @param el {HTMLElement} The canvas element to initialize. 96 * @param {HTMLElement} el The canvas element to initialize. 97 * @return {HTMLElement} the element that was created. 92 98 */ 93 99 initElement: function (el) { … … 100 106 }; 101 107 102 var self = this; //bind 103 el.attachEvent("onpropertychange", function (e) { 104 // we need to watch changes to width and height 105 switch (e.propertyName) { 106 case "width": 107 case "height": 108 // coord size changed? 109 break; 110 } 111 }); 112 113 // if style.height is set 108 // do not use inline function because that will leak memory 109 // el.attachEvent('onpropertychange', onPropertyChange) 110 el.attachEvent('onresize', onResize); 114 111 115 112 var attrs = el.attributes; … … 128 125 } 129 126 }; 127 128 function onPropertyChange(e) { 129 // we need to watch changes to width and height 130 switch (e.propertyName) { 131 case 'width': 132 case 'height': 133 // TODO: coordsize and size 134 break; 135 } 136 } 137 138 function onResize(e) { 139 var el = e.srcElement; 140 if (el.firstChild) { 141 el.firstChild.style.width = el.clientWidth + 'px'; 142 el.firstChild.style.height = el.clientHeight + 'px'; 143 } 144 } 130 145 131 146 G_vmlCanvasManager_.init(); … … 216 231 * This class implements CanvasRenderingContext2D interface as described by 217 232 * the WHATWG. 218 * @param surfaceElement {HTMLElement}The element that the 2D context should233 * @param {HTMLElement} surfaceElement The element that the 2D context should 219 234 * be associated with 220 235 */ 221 236 function CanvasRenderingContext2D_(surfaceElement) { 222 237 this.m_ = createMatrixIdentity(); 223 this.element_ = surfaceElement;224 238 225 239 this.mStack_ = []; … … 236 250 this.miterLimit = 10; 237 251 this.globalAlpha = 1; 252 253 var el = document.createElement('div'); 254 el.style.width = surfaceElement.clientWidth + 'px'; 255 el.style.height = surfaceElement.clientHeight + 'px'; 256 el.style.overflow = 'hidden'; 257 el.style.position = 'absolute'; 258 surfaceElement.appendChild(el); 259 260 this.element_ = el; 238 261 }; 239 262 … … 285 308 } 286 309 287 var xStart = aX + (Math.cos(aStartAngle) * aRadius); 288 var yStart = aY + (Math.sin(aStartAngle) * aRadius); 289 290 var xEnd = aX + (Math.cos(aEndAngle) * aRadius); 291 var yEnd = aY + (Math.sin(aEndAngle) * aRadius); 310 aRadius *= 10; 311 312 var xStart = aX + (mc(aStartAngle) * aRadius) - 5; 313 var yStart = aY + (ms(aStartAngle) * aRadius) - 5; 314 315 var xEnd = aX + (mc(aEndAngle) * aRadius) - 5; 316 var yEnd = aY + (ms(aEndAngle) * aRadius) - 5; 292 317 293 318 this.currentPath_.push({type: "arc", … … 389 414 // For some reason that I've now forgotten, using divs didn't work 390 415 vmlStr.push(' <g_vml_:group', 391 ' coordsize="100 ,100"',416 ' coordsize="1000,1000"', 392 417 ' coordorigin="0, 0"' , 393 418 ' style="width:100px;height:100px;position:absolute;'); … … 419 444 max.y = Math.max(max.y, c2.y, c3.y, c4.y); 420 445 421 vmlStr.push(" padding:0 ", Math.floor(max.x), "px ", Math.floor(max.y),446 vmlStr.push(" padding:0 ", mr(max.x), "px ", mr(max.y), 422 447 "px 0;filter:progid:DXImageTransform.Microsoft.Matrix(", 423 448 filter.join(""), ", sizingmethod='clip');") … … 452 477 ' filled="', Boolean(aFill), '"', 453 478 ' style="position:absolute;width:10;height:10;"', 454 ' coordorigin="0 0" coordsize="10 10"',479 ' coordorigin="0 0" coordsize="100 100"', 455 480 ' stroked="', !aFill, '"', 456 481 ' strokeweight="', this.lineWidth, '"', … … 468 493 lineStr.push(" m "); 469 494 var c = this.getCoords_(p.x, p.y); 470 lineStr.push( Math.floor(c.x), ",", Math.floor(c.y));495 lineStr.push(mr(c.x), ",", mr(c.y)); 471 496 } else if (p.type == "lineTo") { 472 497 lineStr.push(" l "); 473 498 var c = this.getCoords_(p.x, p.y); 474 lineStr.push( Math.floor(c.x), ",", Math.floor(c.y));499 lineStr.push(mr(c.x), ",", mr(c.y)); 475 500 } else if (p.type == "close") { 476 501 lineStr.push(" x "); … … 480 505 var c1 = this.getCoords_(p.cp1x, p.cp1y); 481 506 var c2 = this.getCoords_(p.cp2x, p.cp2y); 482 lineStr.push( Math.floor(c1.x), ",", Math.floor(c1.y), ",",483 Math.floor(c2.x), ",", Math.floor(c2.y), ",",484 Math.floor(c.x), ",", Math.floor(c.y));507 lineStr.push(mr(c1.x), ",", mr(c1.y), ",", 508 mr(c2.x), ",", mr(c2.y), ",", 509 mr(c.x), ",", mr(c.y)); 485 510 } else if (p.type == "arc") { 486 511 lineStr.push(" ar "); … … 495 520 var absYScale = this.m_[1][1]; 496 521 497 lineStr.push( Math.floor(c.x - absXScale * p.radius), ",",498 Math.floor(c.y - absYScale * p.radius), " ",499 Math.floor(c.x + absXScale * p.radius), ",",500 Math.floor(c.y + absYScale * p.radius), " ",501 Math.floor(cStart.x), ",", Math.floor(cStart.y), " ",502 Math.floor(cEnd.x), ",", Math.floor(cEnd.y));522 lineStr.push(mr(c.x - absXScale * p.radius), ",", 523 mr(c.y - absYScale * p.radius), " ", 524 mr(c.x + absXScale * p.radius), ",", 525 mr(c.y + absYScale * p.radius), " ", 526 mr(cStart.x), ",", mr(cStart.y), " ", 527 mr(cEnd.x), ",", mr(cEnd.y)); 503 528 } 504 529 … … 532 557 var dimension = (width > height) ? width : height; 533 558 534 focus.x = Math.floor((this.fillStyle.focus_.x / width) * 100 + 50) + "%";535 focus.y = Math.floor((this.fillStyle.focus_.y / height) * 100 + 50) + "%";559 focus.x = mr((this.fillStyle.focus_.x / width) * 100 + 50) + "%"; 560 focus.y = mr((this.fillStyle.focus_.y / height) * 100 + 50) + "%"; 536 561 537 562 var colors = []; … … 615 640 contextPrototype.getCoords_ = function(aX, aY) { 616 641 return { 617 x: (aX * this.m_[0][0] + aY * this.m_[1][0] + this.m_[2][0]),618 y: (aX * this.m_[0][1] + aY * this.m_[1][1] + this.m_[2][1])642 x: 10 * (aX * this.m_[0][0] + aY * this.m_[1][0] + this.m_[2][0]) - 5, 643 y: 10 * (aX * this.m_[0][1] + aY * this.m_[1][1] + this.m_[2][1]) - 5 619 644 } 620 645 }; … … 644 669 645 670 contextPrototype.rotate = function(aRot) { 646 var c = Math.cos(aRot);647 var s = Math.sin(aRot);671 var c = mc(aRot); 672 var s = ms(aRot); 648 673 649 674 var m1 = [




Atom Feed for the Blog Entries