<i id='wOVNw'><tr id='wOVNw'><dt id='wOVNw'><q id='wOVNw'><span id='wOVNw'><b id='wOVNw'><form id='wOVNw'><ins id='wOVNw'></ins><ul id='wOVNw'></ul><sub id='wOVNw'></sub></form><legend id='wOVNw'></legend><bdo id='wOVNw'><pre id='wOVNw'><center id='wOVNw'></center></pre></bdo></b><th id='wOVNw'></th></span></q></dt></tr></i><div id='wOVNw'><tfoot id='wOVNw'></tfoot><dl id='wOVNw'><fieldset id='wOVNw'></fieldset></dl></div>

    <small id='wOVNw'></small><noframes id='wOVNw'>

    <tfoot id='wOVNw'></tfoot>
      <bdo id='wOVNw'></bdo><ul id='wOVNw'></ul>
    1. <legend id='wOVNw'><style id='wOVNw'><dir id='wOVNw'><q id='wOVNw'></q></dir></style></legend>

        将画布内容序列化为 ArrayBuffer 并再次反序列化

        Serialize canvas content to ArrayBuffer and deserialize again(将画布内容序列化为 ArrayBuffer 并再次反序列化)
        • <small id='qkNRC'></small><noframes id='qkNRC'>

            <tbody id='qkNRC'></tbody>
        • <tfoot id='qkNRC'></tfoot>

          <legend id='qkNRC'><style id='qkNRC'><dir id='qkNRC'><q id='qkNRC'></q></dir></style></legend>

            • <bdo id='qkNRC'></bdo><ul id='qkNRC'></ul>
                1. <i id='qkNRC'><tr id='qkNRC'><dt id='qkNRC'><q id='qkNRC'><span id='qkNRC'><b id='qkNRC'><form id='qkNRC'><ins id='qkNRC'></ins><ul id='qkNRC'></ul><sub id='qkNRC'></sub></form><legend id='qkNRC'></legend><bdo id='qkNRC'><pre id='qkNRC'><center id='qkNRC'></center></pre></bdo></b><th id='qkNRC'></th></span></q></dt></tr></i><div id='qkNRC'><tfoot id='qkNRC'></tfoot><dl id='qkNRC'><fieldset id='qkNRC'></fieldset></dl></div>
                2. 本文介绍了将画布内容序列化为 ArrayBuffer 并再次反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有两个画布,我想把canvas1的内容传过来,序列化到一个ArrayBuffer中,然后加载到canvas2中.以后我会把canvas1的内容发送到服务器,处理后返回给canvas2,但是现在只想序列化和反序列化.

                  I have two canvases, and I want to pass the content of canvas1, serialize it to an ArrayBuffer, and then load it in canvas2. In the future I will send the canvas1 content to the server, process it, and return it to canvas2, but right now I just want to serialize and deserialize it.

                  我发现了这种以字节为单位获取画布信息的方法:

                  I found this way of getting the canvas info in bytes:

                  var img1 = context.getImageData(0, 0, 400, 320);
                  var binary = new Uint8Array(img1.data.length);
                  for (var i = 0; i < img1.data.length; i++) {
                      binary[i] = img1.data[i];
                  }
                  

                  还发现了这种将信息设置为Image对象的方法:

                  And also found this way of set the information to a Image object:

                  var blob = new Blob( [binary], { type: "image/png" } );
                  var urlCreator = window.URL || window.webkitURL;
                  var imageUrl = urlCreator.createObjectURL( blob );
                  var img = new Image();
                  img.src = imageUrl;
                  

                  但不幸的是,它似乎不起作用.

                  But unfortunately it doesn't seem to work.

                  这样做的正确方法是什么?

                  Which would be the right way of doing this?

                  推荐答案

                  您从 getImageData() 获得的 ImageData 已经在使用 ArrayBuffer(由 Uint8ClampedArray 视图使用).抓住它并发送它:

                  The ImageData you get from getImageData() is already using an ArrayBuffer (used by the Uint8ClampedArray view). Just grab it and send it:

                  var imageData = context.getImageData(x, y, w, h);
                  var buffer = imageData.data.buffer;  // ArrayBuffer
                  

                  重新设置:

                  var imageData = context.createImageData(w, h);
                  imageData.data.set(incomingBuffer);
                  

                  您可能想要考虑某种形式的字节编码(例如 f.ex base-64),因为任何高于 127 (ASCII) 的字节值都受系统上使用的字符编码的影响.或者确保旅途中的所有步骤都使用相同的(例如 UTF8).

                  You probably want to consider some form of byte encoding though (such as f.ex base-64) as any byte value above 127 (ASCII) is subject to character encoding used on a system. Or make sure all steps on the trip uses the same (f.ex. UTF8).

                  这篇关于将画布内容序列化为 ArrayBuffer 并再次反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How does object-fit work with canvas element?(对象适合如何与画布元素一起使用?)
                  How to make rooftext effect and valley text effect in HTML5 (or Fabric.js)(如何在 HTML5(或 Fabric.js)中制作屋顶文字效果和山谷文字效果)
                  Draw border around nontransparent part of image on canvas(在画布上的图像不透明部分周围绘制边框)
                  dragging and resizing an image on html5 canvas(在 html5 画布上拖动图像并调整其大小)
                  What#39;s the difference between a boolean as primitive and a boolean as property of an object?(作为原始对象的布尔值和作为对象属性的布尔值有什么区别?)
                  I want to do animation of an object along a particular path(我想沿特定路径对对象进行动画处理)

                  1. <i id='BNCyT'><tr id='BNCyT'><dt id='BNCyT'><q id='BNCyT'><span id='BNCyT'><b id='BNCyT'><form id='BNCyT'><ins id='BNCyT'></ins><ul id='BNCyT'></ul><sub id='BNCyT'></sub></form><legend id='BNCyT'></legend><bdo id='BNCyT'><pre id='BNCyT'><center id='BNCyT'></center></pre></bdo></b><th id='BNCyT'></th></span></q></dt></tr></i><div id='BNCyT'><tfoot id='BNCyT'></tfoot><dl id='BNCyT'><fieldset id='BNCyT'></fieldset></dl></div>
                      <legend id='BNCyT'><style id='BNCyT'><dir id='BNCyT'><q id='BNCyT'></q></dir></style></legend>
                      • <bdo id='BNCyT'></bdo><ul id='BNCyT'></ul>
                          <tfoot id='BNCyT'></tfoot>
                          1. <small id='BNCyT'></small><noframes id='BNCyT'>

                              <tbody id='BNCyT'></tbody>