Mask.hx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import h2d.RenderContext;
  2. import h2d.Object;
  3. class Mask extends hxd.App {
  4. var obj : h2d.Object;
  5. var mask : h2d.Mask;
  6. var time : Float = 0.;
  7. var apiMask : MaskWithSample;
  8. override function init() {
  9. mask = new h2d.Mask(160, 160, s2d);
  10. mask.x = 20;
  11. mask.y = 50;
  12. // Mask-sized rectangle to display mask boundaries
  13. // and make scroll movement more apparent.
  14. new h2d.Bitmap(h2d.Tile.fromColor(0x222222, mask.width, mask.height), mask);
  15. // Limit scroll
  16. mask.scrollBounds = h2d.col.Bounds.fromValues(-mask.width/2, -mask.height/2,mask.width*2, mask.height*2);
  17. obj = new h2d.Object(mask);
  18. obj.x = obj.y = 80;
  19. for( i in 0...10 ) {
  20. var b = new h2d.Bitmap(hxd.Res.hxlogo.toTile(), obj);
  21. b.scale(0.4);
  22. b.x = Std.random(200) - 100;
  23. b.y = Std.random(200) - 100;
  24. }
  25. var info = new h2d.Text(hxd.res.DefaultFont.get(), s2d);
  26. info.setPosition(5, 5);
  27. info.text = "Arrows: move scrollX/Y\nSpace: reset scroll to 0,0";
  28. // Content masking also possible with advanced `Mask.maskWith` and `Mask.unmask` methods.
  29. apiMask = new MaskWithSample(s2d);
  30. apiMask.setPosition(200, 50);
  31. }
  32. override function update(dt:Float) {
  33. time += dt;
  34. obj.rotation += 0.6 * dt;
  35. apiMask.sx = Math.cos(time) * 100 + 100;
  36. apiMask.sy = Math.sin(time) * 100 + 100;
  37. if (hxd.Key.isDown(hxd.Key.LEFT)) mask.scrollX -= 100 * dt;
  38. if (hxd.Key.isDown(hxd.Key.RIGHT)) mask.scrollX += 100 * dt;
  39. if (hxd.Key.isDown(hxd.Key.UP)) mask.scrollY -= 100 * dt;
  40. if (hxd.Key.isDown(hxd.Key.DOWN)) mask.scrollY += 100 * dt;
  41. if (hxd.Key.isReleased(hxd.Key.SPACE)) mask.scrollTo(0, 0);
  42. }
  43. static function main() {
  44. hxd.Res.initEmbed();
  45. new Mask();
  46. }
  47. }
  48. class MaskWithSample extends Object {
  49. public var sx:Float = 0;
  50. public var sy:Float = 0;
  51. public function new(?parent:Object) {
  52. super(parent);
  53. new h2d.Bitmap(hxd.Res.hxlogo.toTile(), this);
  54. }
  55. override function drawRec(ctx:RenderContext)
  56. {
  57. // Masking with advanced API allows to mask contents with objects that cannot use Mask as intermediary or extend from it.
  58. // For practical usage sample see h2d.Flow Hidden overflow mode.
  59. h2d.Mask.maskWith(ctx, this, 50, 50, sx, sy);
  60. super.drawRec(ctx);
  61. // Unmask should be called after `maskWith` in order to restore previous renderZone state.
  62. h2d.Mask.unmask(ctx);
  63. }
  64. }