javascript - 如何使用javascript清除 Canvas 层并将其另存为图像

标签 javascript jquery canvas kineticjs

我正在尝试使用 kinetic-v4.7.2.min.js 在 Canvas 上绘画.当用户触摸该层时,它会成功绘制线条。我为此功能使用 jsFiddle。 我如何清除这个 ractangle 层以及如何将该层容器保存为图像?

JSFiddle

// create a stage and a layer
var stage = new Kinetic.Stage({
    container: 'container',
    width: 350,
    height: 350
});
var layer = new Kinetic.Layer();
stage.add(layer);

// an empty stage does not emit mouse-events
// so fill the stage with a background rectangle
// that can emit mouse-events
var background = new Kinetic.Rect({
    x: 0,
    y: 0,
    width: stage.getWidth(),
    height: stage.getHeight(),
    fill: 'white',
    stroke: 'black',
    strokeWidth: 1,
})
layer.add(background);
layer.draw();

// a flag we use to see if we're dragging the mouse
var isMouseDown = false;
// a reference to the line we are currently drawing
var newline;
// a reference to the array of points making newline
var points = [];

// on the background
// listen for mousedown, mouseup and mousemove events
background.on('mousedown', function () {
    onMousedown();
});
background.on('mouseup', function () {
    onMouseup();
});
background.on('mousemove', function () {
    onMousemove();
});

// On mousedown
// Set the isMouseDown flag to true
// Create a new line,
// Clear the points array for new points
// set newline reference to the newly created line
function onMousedown(event) {
    isMouseDown = true;
    points = [];
    points.push(stage.getMousePosition());
    var line = new Kinetic.Line({
        points: points,
        stroke: "green",
        strokeWidth: 5,
        lineCap: 'round',
        lineJoin: 'round'
    });
    layer.add(line);
    newline = line;
}

// on mouseup end the line by clearing the isMouseDown flag
function onMouseup(event) {
    isMouseDown = false;
}

// on mousemove
// Add the current mouse position to the points[] array
// Update newline to include all points in points[]
// and redraw the layer
function onMousemove(event) {
    if (!isMouseDown) {
        return;
    };
    points.push(stage.getMousePosition());
    newline.setPoints(points);
    // use layer.drawScene
    // this is faster since the "hit" canvas is not refreshed
    layer.drawScene();
}

如有任何帮助,我们将不胜感激!

谢谢。

最佳答案

使用 toDataURL() 方法:

DEMO

$('button').on('click', function () {
    var img = $('.kineticjs-content').find('canvas').get(0).toDataURL("image/png");

    $('body').prepend('<img src="' + img + '">');
});

关于javascript - 如何使用javascript清除 Canvas 层并将其另存为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20263094/

相关文章:

javascript - 单击重置 setInterval

javascript - 如何使用 setInterval 为输入赋值

javascript - Canvas .fillText 方法似乎在 iPad 上不起作用

javascript - 以高分辨率渲染 html Canvas 图像

javascript - 如何检查对象属性的任何值是否等于 0?

javascript - ng-click inside ui-gmap-windows 不工作

javascript - 更新不同状态下的 ui-router 解析数据

javascript - 如果图像没有 src,则使用 jquery 隐藏图像

javascript - Android webview javascript 未在所有设备上运行

javascript - 我在哪里可以找到有关新 Canvas HTML 元素如何工作的有用信息?