gradient.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.gfx3d.gradient"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.gfx3d.gradient"] = true;
  8. dojo.provide("dojox.gfx3d.gradient");
  9. dojo.require("dojox.gfx3d.vector");
  10. dojo.require("dojox.gfx3d.matrix");
  11. (function(){
  12. var dist = function(a, b){ return Math.sqrt(Math.pow(b.x - a.x, 2) + Math.pow(b.y - a.y, 2)); };
  13. var N = 32;
  14. dojox.gfx3d.gradient = function(model, material, center, radius, from, to, matrix){
  15. // summary: calculate a cylindrical gradient
  16. // model: dojox.gfx3d.lighting.Model: color model
  17. // material: Object: defines visual properties
  18. // center: Object: center of the cylinder's bottom
  19. // radius: Number: radius of the cylinder
  20. // from: Number: from position in radians
  21. // to: Number: from position in radians
  22. // matrix: dojox.gfx3d.Matrix3D: the cumulative transformation matrix
  23. // tolerance: Number: tolerable diffirence in colors between gradient steps
  24. var m = dojox.gfx3d.matrix, v = dojox.gfx3d.vector, mx = m.normalize(matrix),
  25. f = m.multiplyPoint(mx, radius * Math.cos(from) + center.x, radius * Math.sin(from) + center.y, center.z),
  26. t = m.multiplyPoint(mx, radius * Math.cos(to) + center.x, radius * Math.sin(to) + center.y, center.z),
  27. c = m.multiplyPoint(mx, center.x, center.y, center.z), step = (to - from) / N, r = dist(f, t) / 2,
  28. mod = model[material.type], fin = material.finish, pmt = material.color,
  29. colors = [{offset: 0, color: mod.call(model, v.substract(f, c), fin, pmt)}];
  30. for(var a = from + step; a < to; a += step){
  31. var p = m.multiplyPoint(mx, radius * Math.cos(a) + center.x, radius * Math.sin(a) + center.y, center.z),
  32. df = dist(f, p), dt = dist(t, p);
  33. colors.push({offset: df / (df + dt), color: mod.call(model, v.substract(p, c), fin, pmt)});
  34. }
  35. colors.push({offset: 1, color: mod.call(model, v.substract(t, c), fin, pmt)});
  36. return {type: "linear", x1: 0, y1: -r, x2: 0, y2: r, colors: colors};
  37. };
  38. })();
  39. }