我想给出一个包含canvas,可视控制器的div,当用jQuery的resizable()函数调整大小时。如何保存并移除画布状态?
我说“视觉控制器”当你点击一个形象,让你来调整这样的例子在图像上出现这8个黑色方块的意思是:
我写了下面的功能围绕div绘制8个方格。点击div时,它会给出目标视觉外观。再次点击div时,它将删除8平方,并删除可调整大小的()函数。它工作正常但当调整div并再次点击它删除8个方块它不会删除它们。
我需要在应用图纸之前保存画布状态,然后再次单击画布时将其恢复。
$(document).on("click", "canvas", function(eg) {
var thisCanvas = $(this).attr("id");
var theCanvas = document.getElementById(thisCanvas);
var ctx = theCanvas.getContext("2d");
canvasSelected(thisCanvas, ctx);
});
当触发以下功能画布的用户点击:
function canvasSelected(theCanvas, ctx){
var ctxWidth = $("#"+theCanvas).width();
var ctxHeight = $("#"+theCanvas).height();
if($("#"+theCanvas).hasClass("bordering")){
ctx.restore();
$("#"+theCanvas).addClass("defaultBorder");
$("#"+theCanvas).removeClass("bordering");
ctx.beginPath();
ctx.clearRect(0,0,6,6);
ctx.clearRect(ctxWidth- 6,0,6,6);
ctx.clearRect(ctxWidth/2,0,6,6);
ctx.clearRect(0,ctxHeight- 6,6,6);
ctx.clearRect(ctxWidth- 6,ctxHeight- 6,6,6);
ctx.clearRect(ctxWidth/2,ctxHeight- 6,6,6);
ctx.clearRect(0,ctxHeight/2,6,6);
ctx.clearRect(ctxWidth- 6,ctxHeight/2,6,6);
$("#"+theCanvas).resizable("option", "disabled", true);
}else{
ctx.save();
$("#"+theCanvas).removeClass("defaultBorder");
$("#"+theCanvas).addClass("bordering");
ctx.beginPath();
ctx.fillStyle = "black";
ctx.fillRect(0,0,6,6);
ctx.fill();
ctx.fillRect(ctxWidth- 6,0,6,6);
ctx.fill();
ctx.fillRect(ctxWidth/2,0,6,6);
ctx.fill();
// bottom rects..
ctx.fillRect(0,ctxHeight- 6,6,6);
ctx.fill();
ctx.fillRect(ctxWidth- 6,ctxHeight- 6,6,6);
ctx.fill();
ctx.fillRect(ctxWidth/2,ctxHeight- 6,6,6);
ctx.fill();
// middle rects
ctx.fillRect(0,ctxHeight/2,6,6);
ctx.fill();
ctx.fillRect(ctxWidth- 6,ctxHeight/2,6,6);
ctx.fill();
$("#"+theCanvas).resizable();
$("#"+theCanvas).resizable("option", "disabled", false);
}
}
这里是jsfiddle
尝试绑定到可调整大小的窗口小部件的停止事件,然后调用canvasSelected。请参阅http://api.jqueryui.com/resizable/#event-stop – bfavaretto 2013-03-08 22:42:58