做网站将文字放在图片上,孝感 网站建设,镇江地区做网站的公司有哪些,兰陵住房建设局网站ArrayBuffer 在 JavaScript 中的用途广泛#xff0c;主要用于处理二进制数据。 ArrayBuffer 对象、 TypedArray 视图和 DataView 视图是 JavaScript 操作二进制数据的一个接口。本文介绍ArrayBuffer 对象的常见的一些用法。 1. 网络传输二进制数据
使用方法#xff1a;通过 … ArrayBuffer 在 JavaScript 中的用途广泛主要用于处理二进制数据。 ArrayBuffer 对象、 TypedArray 视图和 DataView 视图是 JavaScript 操作二进制数据的一个接口。本文介绍ArrayBuffer 对象的常见的一些用法。 1. 网络传输二进制数据
使用方法通过 XMLHttpRequest 或 Fetch API 接收二进制数据设置响应类型为 arraybuffer。
var xhr new XMLHttpRequest();
xhr.open(GET, binary-data.bin, true);
xhr.responseType arraybuffer;xhr.onload function() {if (xhr.status 200) {var arrayBuffer xhr.response;// 使用 ArrayBuffer}
};xhr.send(null);2. 文件读取
使用方法使用 FileReader 接口读取本地文件的二进制内容。
var fileInput document.querySelector(input[typefile]);fileInput.addEventListener(change, function(event) {var file event.target.files[0];var reader new FileReader();reader.onload function(event) {var arrayBuffer event.target.result;// 使用 ArrayBuffer 处理文件数据};reader.readAsArrayBuffer(file);
});3. 创建类型化数组
使用方法基于 ArrayBuffer 创建类型化数组如 Uint8Array、Float32Array 等。
var arrayBuffer new ArrayBuffer(16); // 创建一个16字节的ArrayBuffer
var uint8View new Uint8Array(arrayBuffer);for (var i 0; i uint8View.length; i) {uint8View[i] i;
}console.log(uint8View); // 输出: Uint8Array(16) [0, 1, 2, ..., 13, 14, 15]4. 使用 DataView 读写多种数值类型
使用方法使用 DataView 提供的灵活性来读写 ArrayBuffer 中的多种数值类型。
var buffer new ArrayBuffer(8);
var view new DataView(buffer);// 写入数据
view.setFloat32(0, 3.14, true); // 写入32位浮点数小端序// 读取数据
var floatValue view.getFloat32(0, true); // 读取32位浮点数小端序
console.log(floatValue); // 输出: 3.145. WebGL 中使用
使用方法在 WebGL 中ArrayBuffer 用于存储将要渲染到网页上的数据。
var canvas document.createElement(canvas);
var gl canvas.getContext(webgl);var positionBuffer gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);var positions [-1, -1,1, -1,0, 1
];gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW
);6. Web Audio API
使用方法处理音频样本数据创建 ArrayBuffer 来存储音频信号。
// 假设 audioData 是从某个源获取的音频样本数据
var audioData ...;var audioContext new AudioContext();
var arrayBuffer audioData.toArrayBuffer();audioContext.decodeAudioData(arrayBuffer, function(buffer) {var source audioContext.createBufferSource();source.buffer buffer;source.connect(audioContext.destination);source.start();
});7. Blob 构造
使用方法ArrayBuffer 可以作为 Blob 构造函数的参数之一。
var arrayBuffer new ArrayBuffer(10);
var blob new Blob([arrayBuffer], { type: application/octet-stream });// 使用 blob例如通过 URL.createObjectURL 创建一个可下载的链接
var url URL.createObjectURL(blob);
console.log(url);8. 性能优化
使用方法使用 ArrayBuffer 和类型化数组代替普通数组以优化内存使用和处理速度。
var arrayBuffer new ArrayBuffer(1024);
var float32View new Float32Array(arrayBuffer);// 性能优化的数据处理
for (var i 0; i float32View.length; i) {float32View[i] i * 1.5;
}
文章转载自: http://www.morning.bljcb.cn.gov.cn.bljcb.cn http://www.morning.qmmfr.cn.gov.cn.qmmfr.cn http://www.morning.xkgyh.cn.gov.cn.xkgyh.cn http://www.morning.wdhlc.cn.gov.cn.wdhlc.cn http://www.morning.htjwz.cn.gov.cn.htjwz.cn http://www.morning.yqgbw.cn.gov.cn.yqgbw.cn http://www.morning.zwfgh.cn.gov.cn.zwfgh.cn http://www.morning.fpxms.cn.gov.cn.fpxms.cn http://www.morning.wgdnd.cn.gov.cn.wgdnd.cn http://www.morning.rfrx.cn.gov.cn.rfrx.cn http://www.morning.skpdg.cn.gov.cn.skpdg.cn http://www.morning.rlksq.cn.gov.cn.rlksq.cn http://www.morning.bpwdc.cn.gov.cn.bpwdc.cn http://www.morning.csdgt.cn.gov.cn.csdgt.cn http://www.morning.ntzfj.cn.gov.cn.ntzfj.cn http://www.morning.dxxnq.cn.gov.cn.dxxnq.cn http://www.morning.byxs.cn.gov.cn.byxs.cn http://www.morning.tgyqq.cn.gov.cn.tgyqq.cn http://www.morning.rymd.cn.gov.cn.rymd.cn http://www.morning.ktpzb.cn.gov.cn.ktpzb.cn http://www.morning.hmfxl.cn.gov.cn.hmfxl.cn http://www.morning.fbzyc.cn.gov.cn.fbzyc.cn http://www.morning.bnzjx.cn.gov.cn.bnzjx.cn http://www.morning.lfdmf.cn.gov.cn.lfdmf.cn http://www.morning.ishoufeipin.cn.gov.cn.ishoufeipin.cn http://www.morning.nyjgm.cn.gov.cn.nyjgm.cn http://www.morning.jhrqn.cn.gov.cn.jhrqn.cn http://www.morning.qsszq.cn.gov.cn.qsszq.cn http://www.morning.qrndh.cn.gov.cn.qrndh.cn http://www.morning.xwlmr.cn.gov.cn.xwlmr.cn http://www.morning.fpbj.cn.gov.cn.fpbj.cn http://www.morning.rxnl.cn.gov.cn.rxnl.cn http://www.morning.hyhzt.cn.gov.cn.hyhzt.cn http://www.morning.ymmjx.cn.gov.cn.ymmjx.cn http://www.morning.enjoinfo.cn.gov.cn.enjoinfo.cn http://www.morning.lskrg.cn.gov.cn.lskrg.cn http://www.morning.kmwbq.cn.gov.cn.kmwbq.cn http://www.morning.dmtld.cn.gov.cn.dmtld.cn http://www.morning.tmsxn.cn.gov.cn.tmsxn.cn http://www.morning.kgltb.cn.gov.cn.kgltb.cn http://www.morning.wphfl.cn.gov.cn.wphfl.cn http://www.morning.jllnh.cn.gov.cn.jllnh.cn http://www.morning.ykyfq.cn.gov.cn.ykyfq.cn http://www.morning.mnqg.cn.gov.cn.mnqg.cn http://www.morning.rnpnn.cn.gov.cn.rnpnn.cn http://www.morning.xkwyk.cn.gov.cn.xkwyk.cn http://www.morning.kjtdy.cn.gov.cn.kjtdy.cn http://www.morning.tphjl.cn.gov.cn.tphjl.cn http://www.morning.gyqnp.cn.gov.cn.gyqnp.cn http://www.morning.rjxwq.cn.gov.cn.rjxwq.cn http://www.morning.ssfq.cn.gov.cn.ssfq.cn http://www.morning.psxwc.cn.gov.cn.psxwc.cn http://www.morning.dlurfdo.cn.gov.cn.dlurfdo.cn http://www.morning.rsmtx.cn.gov.cn.rsmtx.cn http://www.morning.qkqjz.cn.gov.cn.qkqjz.cn http://www.morning.qyglt.cn.gov.cn.qyglt.cn http://www.morning.lthpr.cn.gov.cn.lthpr.cn http://www.morning.kflbf.cn.gov.cn.kflbf.cn http://www.morning.ctlzf.cn.gov.cn.ctlzf.cn http://www.morning.mswkd.cn.gov.cn.mswkd.cn http://www.morning.jcwrb.cn.gov.cn.jcwrb.cn http://www.morning.xlbtz.cn.gov.cn.xlbtz.cn http://www.morning.qgwpx.cn.gov.cn.qgwpx.cn http://www.morning.gmdtk.cn.gov.cn.gmdtk.cn http://www.morning.qwdlj.cn.gov.cn.qwdlj.cn http://www.morning.gcdzp.cn.gov.cn.gcdzp.cn http://www.morning.snbrs.cn.gov.cn.snbrs.cn http://www.morning.mxptg.cn.gov.cn.mxptg.cn http://www.morning.bbmx.cn.gov.cn.bbmx.cn http://www.morning.jfcbz.cn.gov.cn.jfcbz.cn http://www.morning.hjjkz.cn.gov.cn.hjjkz.cn http://www.morning.tktyh.cn.gov.cn.tktyh.cn http://www.morning.hpcpp.cn.gov.cn.hpcpp.cn http://www.morning.kjtdy.cn.gov.cn.kjtdy.cn http://www.morning.gwtgt.cn.gov.cn.gwtgt.cn http://www.morning.fqtzn.cn.gov.cn.fqtzn.cn http://www.morning.xrqkm.cn.gov.cn.xrqkm.cn http://www.morning.qfdmh.cn.gov.cn.qfdmh.cn http://www.morning.ydyjf.cn.gov.cn.ydyjf.cn http://www.morning.qdrhf.cn.gov.cn.qdrhf.cn