LoginSignup
1

More than 5 years have passed since last update.

img要素の画像をデータURIスキームに変換する。

Last updated at Posted at 2017-10-03

画像ファイルを取得してcanvasに転写し、DataURIsに変換して差し替えるスニペット。


d3.selectAll("img")
    .each(function(){
        var canvas = document.createElement('canvas');
        canvas.width = this.clientWidth; 
        canvas.height = this.clientHeight;         
        canvas.getContext('2d').drawImage(this, 0, 0);

        var dataURI = canvas.toDataURL('image/png');
        d3.select(this).attr("src", dataURI);

        d3.select(canvas).remove();

});

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1