顯示具有 html 標籤的文章。 顯示所有文章
顯示具有 html 標籤的文章。 顯示所有文章

2012/10/1

移除畫面上的 flash 時清除 flash 所占用的記憶體?


=== 目標 ===



  移除畫面上的 flash 時能夠清除 flash 所占用的記憶體


=== 實驗 ===



假設操作 HTML 字串等於操作記憶體
新增 Object Tag 增加記憶體使用量
移除 Object Tag 減少記憶體使用量


畫面上放兩個按鈕
  addFlash
  removeFlash

按一下 addFlash 新增一個 Object Tag
按一下 removeFlash 移除所有 Object Tag


=== 實作 ===


  === HTML的部分 ===


<a href="#" id="addFlash">add flash</a>
<a href="#" id="removeFlash">remove flash</a>
<div id="flashContainer"></div>


  === js的部分 ===

  $(function(){
      $("#addFlash").click(function(){
          var random  = 'a' + Math.floor(Math.random() * 1000000000);
          $("#flashContainer").append('<div id="'+ random+ '" />');
          swfobject.embedSWF("test.swf", random , "908", "560", "10.0.0");
      });

      $("#removeFlash").click(function(){
          $("#flashContainer").html("");
      });
  });


=== 實驗結果 ===



chrome, firefox, safari在按下removeFlash時
系統記憶體使用量在1秒內明顯下降,好像沒按過addFlsah一樣

但是 IE 不意外,在按下removeFlash時
系統記憶體使用量開始緩緩下降,過了一分鐘還沒降到該降到的位置

不知道 IE 怎麼達到移除 flash 時釋放記憶體?

(其實也不能說他沒有釋放,只是他釋放得很慢)

2012/3/9

HTML5 Canvas eraser demo using EaselJS

原理
  html5 canvas 的橡皮擦怎麼作?
  用context.globalCompositeOperation
  根據Canvas Composite Operations Tutorial & Demo
  我們知道只要寫

    context.globalCompositeOperation = "destination-out"

  就可以作出橡皮擦功能
  但我們不直接用他
  因為我們要透過 EaselJS 來操作 canvas
  所以我們變成要寫

  DisplayObject.compositeOperation = "destination-out";

demo

code

<html>
<head>
    <title></title>
    <script type="text/javascript" src="../lib/easel.js"></script>
    <script type="text/javascript" src="../lib/jquery-1.7.1.js"></script>
    <script type="text/javascript">
        $(function(){
            var canvas = $("#canvas")[0];
            var stage = new Stage(canvas);
            var shape1 = new Shape();
            var shape2 = new Shape();
            var color = "rgba(255,0,0,1)";
            shape1.graphics.setStrokeStyle(16, "round", "round")
                    .beginStroke(color)
                    .moveTo(100, 300)
                    .lineTo(300, 100)
                    .endStroke();
            shape2.graphics.setStrokeStyle(16, "round", "round")
                    .beginStroke(color)
                    .moveTo(100, 100)
                    .lineTo(300, 300)
                    .endStroke();
            shape2.compositeOperation = "destination-out";
            stage.addChild(shape1);
            stage.addChild(shape2);
            stage.update();
        });
    </script>
    </head>
<body>
<canvas id="canvas" width="640" height="480"></canvas>
</body>
</html>

這是用 http://tohtml.com/ 貼code的功能,感覺還不錯

html canvas link

Canvas Composite Operations Tutorial & Demo (混色模式教學)
http://mrcoles.com/blog/canvas-composite-operations-demo-animation/

EaselJS新手上路
http://blog.eddie.com.tw/2011/09/10/introduction-to-easeljs/

HTML5 Canvas eraser demo using EaselJS (橡皮擦教學)
http://etrex.blogspot.com/2012/03/html5-canvas-raser-demo-using-easeljs.html