首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Famo.us脱机显示图像

如何使用Famo.us脱机显示图像
EN

Stack Overflow用户
提问于 2015-03-18 17:19:46
回答 1查看 66关注 0票数 0

如何显示可以使用Famo.us脱机使用的图像?

Famo.us图像曲面基于图像URL设置其内容。但是,当脱机时,此URL将无法工作。我尝试使用HTTP请求并将缓冲区转换为Blob,然后为Blob创建一个URI:

代码语言:javascript
复制
    // bytes = request.buffer of HTTP request
    // Make a Blob from the bytes
    var blob = new Blob([bytes], {type: 'image/jpg'});

    // Use createObjectURL to make a URL for the blob
    imageURL = URL.createObjectURL(blob);

    //Use Famo.us image surface API to display the image (http://famo.us/docs/api/latest/surfaces/ImageSurface)
    imageSurface.setContent(imageURL);

这不会引发任何警告或错误,但会显示一个损坏的图像。有什么建议吗?我想显示一个图像,即使我离线,但当我重新连接,我将请求最新的图像上传。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-18 22:44:41

ImageSurface in Famo.us可以对图像或基-64编码的数据获取一个url。

使用Blob

将文件读取器用于readAsDataURL

代码语言:javascript
复制
 var reader  = new FileReader();

  reader.onloadend = function () {
    imageURL = reader.result;
  }

  reader.readAsDataURL(blob);

使用Canvas

有很多方法可以获得编码的基数-64数据。Canvas还有另一种方法为您获取此字符串。

这里的示例jsBin

使用下面的示例片段,您可以看到如何使用Canvas.toDataURL

代码语言:javascript
复制
  var image = new Image();
  image.crossOrigin = 'anonymous'; // only needed for CORS, not needed for same domain.

  // create an empty canvas element
  var canvas = document.createElement("canvas");
  var canvasContext = canvas.getContext("2d");

  image.onload = function () {

    //Set canvas size is same as the picture
    canvas.width = image.width;
    canvas.height = image.height;

    // draw image into canvas element
    canvasContext.drawImage(image, 0, 0, image.width, image.height);

    // get canvas contents as a data URL (returns png format by default)
    imageURL  = canvas.toDataURL();
  };

  image.src = 'http://code.famo.us/assets/famous_logo.png';  
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29128611

复制
相关文章

相似问题

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