gradient.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. define("dojox/gfx3d/gradient", ["dojo/_base/lang","./matrix","./vector"],
  2. function(lang,m,v){
  3. var gfx3d = lang.getObject("dojox.gfx3d",true);
  4. var dist = function(a, b){ return Math.sqrt(Math.pow(b.x - a.x, 2) + Math.pow(b.y - a.y, 2)); };
  5. var N = 32;
  6. gfx3d.gradient = function(model, material, center, radius, from, to, matrix){
  7. // summary: calculate a cylindrical gradient
  8. // model: dojox.gfx3d.lighting.Model: color model
  9. // material: Object: defines visual properties
  10. // center: Object: center of the cylinder's bottom
  11. // radius: Number: radius of the cylinder
  12. // from: Number: from position in radians
  13. // to: Number: from position in radians
  14. // matrix: dojox.gfx3d.Matrix3D: the cumulative transformation matrix
  15. // tolerance: Number: tolerable difference in colors between gradient steps
  16. var mx = m.normalize(matrix),
  17. f = m.multiplyPoint(mx, radius * Math.cos(from) + center.x, radius * Math.sin(from) + center.y, center.z),
  18. t = m.multiplyPoint(mx, radius * Math.cos(to) + center.x, radius * Math.sin(to) + center.y, center.z),
  19. c = m.multiplyPoint(mx, center.x, center.y, center.z), step = (to - from) / N, r = dist(f, t) / 2,
  20. mod = model[material.type], fin = material.finish, pmt = material.color,
  21. colors = [{offset: 0, color: mod.call(model, v.substract(f, c), fin, pmt)}];
  22. for(var a = from + step; a < to; a += step){
  23. var p = m.multiplyPoint(mx, radius * Math.cos(a) + center.x, radius * Math.sin(a) + center.y, center.z),
  24. df = dist(f, p), dt = dist(t, p);
  25. colors.push({offset: df / (df + dt), color: mod.call(model, v.substract(p, c), fin, pmt)});
  26. }
  27. colors.push({offset: 1, color: mod.call(model, v.substract(t, c), fin, pmt)});
  28. return {type: "linear", x1: 0, y1: -r, x2: 0, y2: r, colors: colors};
  29. };
  30. return gfx3d.gradient;
  31. });