首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在后台设置BPG

如何在后台设置BPG
EN

Stack Overflow用户
提问于 2015-01-22 19:46:22
回答 1查看 1.7K关注 0票数 4

我可以将BPG图像用作<div><body>背景吗?background: url('bg.bpg');不起作用。

有一个Javascript解码器,它允许在<img>标签中显示.bpg图像,但我想使用一个作为<body>背景。

EN

回答 1

Stack Overflow用户

发布于 2015-01-25 18:22:01

Javascript BPG解码器通过将源.bpg文件转换为ImageData对象来工作,然后可以将该对象绘制到画布上。所以你需要做的就是get the canvas as a PNG data string并将其设置为身体背景图像:

代码语言:javascript
复制
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>BPG background example.</title>
        <!-- Decoder script from http://bellard.org/bpg/ -->
        <script src="bpgdec8a.js"></script>
        <script>
"use strict";

function setBackground(filename)
{

  // Create a hidden canvas with the same dimensions as the image.
  var canvas = document.createElement("canvas");
  canvas.style.visibility = "hidden";
  canvas.width = 512;
  canvas.height = 512;
  document.body.appendChild(canvas);

  // Create a drawing context and a BPG decoder.
  var ctx = canvas.getContext("2d");
  var img = new BPGDecoder(ctx);

  // Attempt to load the image.
  img.onload = function()
  {
    // Once the image is ready, draw it to the canvas...
    ctx.putImageData(this.imageData, 0, 0);
    // ...and copy the canvas to the body background.
    document.body.style.background = "url('" + canvas.toDataURL("image/png") + "')";
  };
  img.load(filename);

}
        </script>
    </head>
    <body onload="setBackground('lena512color.bpg');">
        <p>Hello, World!</p>
    </body>
</html>
票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28088036

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档