我有自定义剪辑,而不是默认剪辑。在我的情况下,我想看到剪切区域和水印之外的图像。一切正常,但我不能改变我背后空白区域的背景颜色。
这是有效的fiddle。
我尝试设置c.backgroundColor = 'red'
或c.overlayColor = 'red'
,但它不起作用。
编辑:解决方案的示例可以在this fiddle中找到。
答案 0 :(得分:1)
无效,因为您已将 read_csv('loadTest.csv',
col_types = cols_only('col1' = col_integer(), #col1 is integer
'col2' = 'c', #col2 is character
'col8' = col_guess() #guess type
'col10' = '?' #guess type
)
)
属性设置为 globalCompositeOperation
对象,它更改背景颜色的绘图目标。根本不需要为图像对象设置此属性。
以下是您的代码的工作版本......
destination-atop
var c = new fabric.Canvas('c');
c.setBackgroundColor('white'); //set canvas background color
var clip = {
left: 100,
top: 100,
right: c.getWidth() - 100,
bottom: c.getHeight() - 100
}
var rb = new fabric.Path('M 0 0 H ' + c.getWidth() + ' V ' + clip.top + ' H ' + clip.left + ' V ' + clip.bottom + ' H ' + clip.right + ' V ' + clip.top + ' H ' + c.getWidth() + ' V ' + c.getHeight() + ' H 0 Z', {
left: 0,
top: 0,
fill: 'rgba(100,50,31,0.3)'
});
var circle = new fabric.Circle({
left: clip.right - 40,
top: clip.bottom - 40,
radius: 40,
fill: 'bisque'
});
var g = new fabric.Group([rb, circle], {
left: 0,
top: 0
});
c.setOverlayImage(g);
fabric.Image.fromURL('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAAnFBMVEWZstqNtfdQXXJKXoGNtfiUs+eZstuQtO+astiQtPFNXXiVs+OPtPKPuPtLXn1HWnxJVWlqh7lEVnZ8n9qHncCNpMlbdJ6dt+BicoyJsPBoeZRNYoZbaoKEqedvgZ6Cl7lzk8lSaZBvjsJ+ot1UYnhhfKl1iKd8kLFYcJlmg7JldZCLreV+n9RJW3l3kLlJV29lep1ab5KIptdpgahucM3iAAANtUlEQVR4nO1d7ZLauBI14HWYyWpsDMaYbwYYhiSTbO6+/7tdScZYUrdsCUhWdqyq/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7CLZR7tVadhJsqdxrINheudc08E3KvUaBb1XuNQd8u3KvKeA7lHsNAd+j3GsG+C7lXiPAtco9A8tug/GH4XBY2YIb+IFrS/CNFxkNBzudNx4Bdrd2Pwjsdt54ANiRSOli8Gaw27X7AWBHIqWLwQ7seCroCv3NYEdSQZdkbgY7Eildob8Z7EikdDHYgR1PBb8Q7EY5/oWF3mXl3mPADiv3HgR2V7n3KLC7yr1HgV1V7j0O7Khy74HgTrmXD3fyhj24U+6x4VLesAd3yj3nwsoa3Cn3nAsra3Cn3HPRZztwp9xDLDcM3Cn3gOXGgf9Y5Z6VSMxpcO//6U51eI2AVAQAAAAASUVORK5CYII=', function(fimg) {
c.add(fimg.set({
left: clip.left - 50,
top: clip.top - 50,
//globalCompositeOperation: 'destination-atop' //<-- do not set this
}));
c.setActiveObject(c.item(0));
})
c.renderAll();
fabric.Canvas.prototype.cropImage = function(format, quality, x, y, width, height) {
var canvasEl = this.lowerCanvasEl || this.upperCanvasEl;
this.deactivateAll().renderAll(true);
var printEl = canvasEl;
// Only use extra canvas if any optional param is given
if (x || y || width || height) {
// Defaults
x = x || 0;
y = y || 0;
width = width || canvasEl.width - x;
height = height || canvasEl.height - y;
// create printCanvas if necessary
if (!fabric.printCanvas)
fabric.printCanvas = document.createElement('canvas');
printEl = fabric.printCanvas;
// Calculate spaces over canvas bounds
var d = {
left: (x > 0) ? 0 : -x,
top: (y > 0) ? 0 : -y,
right: (x + width < canvasEl.width) ? 0 : (width + x) - canvasEl.width,
bottom: (y + height < canvasEl.height) ? 0 : (height + y) - canvasEl.height
};
// Print section
printEl.width = width;
printEl.height = height;
printEl.getContext('2d').drawImage(canvasEl,
x + d.left, y + d.top, width - d.right - d.left, height - d.bottom - d.top,
d.left, d.top, width - d.right - d.left, height - d.bottom - d.top);
}
var data = (fabric.StaticCanvas.supports('toDataURLWithQuality')) ? printEl.toDataURL('image/' + format, quality) : printEl.toDataURL('image/' + format);
this.contextTop && this.clearContext(this.contextTop);
this.renderAll();
return data;
};
$('#clip').click(function() {
window.open(c.cropImage('png', 75, clip.left, clip.top, clip.right - clip.left, clip.bottom - clip.top));
});
canvas{outline: 2px solid black;}
和 JSFiddle