Web前端实现多页Tif文件预览

<html>
<meta charset="utf-8" />
<script src="tiff.js"></script>
<script>
var xhr = new XMLHttpRequest();
xhr.responseType = 'arraybuffer';
xhr.open('GET', "http://localhost/1_2.tif");
xhr.onload = function(e){
  Tiff.initialize({ TOTAL_MEMORY :100*1024*1024 });
  var tiff = new Tiff({buffer: xhr.response});
  //输出tiff文件数量
  var canvasList = [];
  var buttons = [];
  var countDirectory = tiff.countDirectory();
  
  for(let i = 0;i < countDirectory;i++){
    tiff.setDirectory(i);
    var canvas = tiff.toCanvas();
    canvasList.push(canvas);
    
    let button = document.createElement("INPUT");
    button.type = "button";
    button.value = "第"+(i + 1)+"页";
    button.style.cssText = "margin-left:"+i * 10 +"px;";
    button.addEventListener("click",(function(n){
        return function(){
            for(let i = 0;i < buttons.length;i++){
                buttons[i].style.fontWeight = "normal";
            }
            buttons[n].style.fontWeight = "bold";
            var tiffcontainer = document.getElementById("tiffcontainer");
            tiffcontainer.innerHTML  = '';
            var canvas = canvasList[n];
            canvas.style.width = tiffcontainer.clientWidth;
            canvas.style.height = tiffcontainer.clientHeight;
            tiffcontainer.append(canvas);
        }
    })(i));
    document.getElementById("buttons").append(button);
    buttons.push(button);
  }
  
  buttons[0].click();
};
xhr.send();
</script>
<body>
前端预览tif文件测试
<div id="buttons">
    
</div>
<div style="width:800px;height:500px;border:1px solid silver;margin-top:10px;" id="tiffcontainer">

</div>
</body>
</html>

tiff.js下载:  git clone https://github.com/seikichi/tiff.js.git

如果不加 Tiff.initialize({ TOTAL_MEMORY :100*1024*1024 }); 控制台会报错 offset is out of bounds

效果如下

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天水麒麟姜伯约

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值