【发布时间】:2020-07-11 10:53:44
【问题描述】:
我正在尝试使用 d3 从 json 文件中绘制图像。然后将此图像数据放入画布上下文中以实际绘制图像。我想旋转生成的图像,但找不到方法。我实际使用的代码
$(document).ready(function() {
d3.json("resources/image.json").then(function(image) {
var canvas = d3.select("colorImage")
.append("canvas")
.attr("width", 200)
.attr("height", 200);
var context = canvas.node().getContext("2d");
var image_data = context.createImageData(200, 200);
image_data.data.set(image);
context.rotate(60);
context.putImageData(image_data, 0, 0); // <---- image not rotated
context.fillRect( 100, 100, 20, 20 ); // <--- rectangle rotated
}).catch(function(error){
if (error) throw error;
});
})
【问题讨论】:
-
你试过这个堆栈溢出答案吗:stackoverflow.com/a/29398757/7548672?
-
是的,但这似乎是我构建图像的方式的问题。我还尝试将其嵌入到“内存中”画布中,但没有成功。
-
你能把你的 json 添加到一个变量中并创建一个功能齐全的 sn-p 吗?
标签: javascript d3.js canvas html5-canvas