dojox/gfx/gradient.js

  • Provides:

    • dojox.gfx.gradient
  • Requires:

    • dojox.gfx.matrix in common
  • p

    • summary
  • dojox.gfx.gradient.rescale

    • type
      Function
    • parameters:
      • stops: (typeof Array)
        input gradient as a list of colors with offsets
        (see dojox.gfx.defaultLinearGradient and dojox.gfx.defaultRadialGradient)
      • from: (typeof Number)
        the beginning of the window, should be less than "to"
      • to: (typeof Number)
        the end of the window, should be more than "from"
    • source: [view]
        var len = stops.length, reverseFlag = (to < from), newStops;


        // do we need to reverse the color table?
        if(reverseFlag){
         var tmp = from;
         from = to;
         to = tmp;
        }

        
        // various edge cases
        if(!len){
         // no colors
         return [];
        }
        if(to <= stops[0].offset){
         // all colors are before the color table
         newStops = [
          {offset: 0, color: stops[0].color},
          {offset: 1, color: stops[0].color}
         ];
        }else if(from >= stops[len - 1].offset){
         // all colors are after the color table
         newStops = [
          {offset: 0, color: stops[len - 1].color},
          {offset: 1, color: stops[len - 1].color}
         ];
        }else{
         // main scanning algorithm
         var span = to - from, stop, prev, i;
         newStops = [];
         if(from < 0){
          newStops.push({offset: 0, color: new C(stops[0].color)});
         }
         for(i = 0; i < len; ++i){
          stop = stops[i];
          if(stop.offset >= from){
           break;
          }
          // skip this color
         }
         if(i){
          prev = stops[i - 1];
          newStops.push({
           offset: 0,
           color: d.blendColors(new C(prev.color), new C(stop.color), (from - prev.offset) / (stop.offset - prev.offset))
          });
         }else{
          newStops.push({offset: 0, color: new C(stop.color)});
         }
         for(; i < len; ++i){
          stop = stops[i];
          if(stop.offset >= to){
           break;
          }
          newStops.push({offset: (stop.offset - from) / span, color: new C(stop.color)});
         }
         if(i < len){
          prev = stops[i - 1];
          newStops.push({
           offset: 1,
           color: d.blendColors(new C(prev.color), new C(stop.color), (to - prev.offset) / (stop.offset - prev.offset))
          });
         }else{
          newStops.push({offset: 1, color: new C(stops[len - 1].color)});
         }
        }

        
        // reverse the color table, if needed
        if(reverseFlag){
         newStops.reverse();
         for(i = 0, len = newStops.length; i < len; ++i){
          stop = newStops[i];
          stop.offset = 1 - stop.offset;
         }
        }

        
        return newStops;
    • summary
      recalculates a gradient from 0-1 window to
      &quot;from&quot;-&quot;to&quot; window blending and replicating colors,
      if necessary
  • dojox.gfx.gradient.project

    • type
      Function
    • parameters:
      • matrix: (typeof dojox.gfx.Matrix2D|Null)
        matrix to apply to a shape and its gradient
      • grad: (typeof Object)
        a linear gradient object to be transformed
      • tl: (typeof dojox.gfx.Point)
        top-left corner of shape's bounding box
      • rb: (typeof dojox.gfx.Point)
        right-bottom corner of shape's bounding box
      • ttl: (typeof dojox.gfx.Point)
        top-left corner of shape's transformed bounding box
      • trb: (typeof dojox.gfx.Point)
        right-bottom corner of shape's transformed bounding box
    • source: [view]
        matrix = matrix || m.identity;


        var f1 = m.multiplyPoint(matrix, grad.x1, grad.y1),
         f2 = m.multiplyPoint(matrix, grad.x2, grad.y2),
         angle = Math.atan2(f2.y - f1.y, f2.x - f1.x),
         project = m.project(f2.x - f1.x, f2.y - f1.y),
         pf1 = m.multiplyPoint(project, f1),
         pf2 = m.multiplyPoint(project, f2),
         shiftAndRotate = new m.Matrix2D([m.rotate(-angle), {dx: -pf1.x, dy: -pf1.y}]),
         scale = m.multiplyPoint(shiftAndRotate, pf2).x,
         //comboMatrix = new m.Matrix2D([shiftAndRotate, project, matrix]),
         // bbox-specific calculations
         points = [
           getPoint(tl.x, tl.y, matrix, project, shiftAndRotate, scale),
           getPoint(rb.x, rb.y, matrix, project, shiftAndRotate, scale),
           getPoint(tl.x, rb.y, matrix, project, shiftAndRotate, scale),
           getPoint(rb.x, tl.y, matrix, project, shiftAndRotate, scale)
          ].sort(sortPoints),
         from = points[0].o,
         to = points[3].o,
         stops = dojox.gfx.gradient.rescale(grad.colors, from, to),
         //angle2 = Math.atan2(Math.abs(points[3].r.y - points[0].r.y) * (f2.y - f1.y), Math.abs(points[3].r.x - points[0].r.x) * (f2.x - f1.x));
         angle2 = Math.atan2(points[3].r.y - points[0].r.y, points[3].r.x - points[0].r.x);


        return {
         type: "linear",
         x1: points[0].p.x, y1: points[0].p.y, x2: points[3].p.x, y2: points[3].p.y,
         colors: stops,
         // additional helpers (for VML)
         angle: angle
        };
    • summary
      return a new gradient using the &quot;VML algorithm&quot; and suitable for VML
  • dojox.gfx.gradient

    • type
      Object
    • summary
  • dojox.gfx

    • type
      Object
    • summary
  • dojox

    • type
      Object
    • summary