我想像下面一样使用jsPDF在剪切区域内设置行。
因此,我调用了blew之类的方法。
我用如下样式参数null调用.lines()后调用了.clip()。
var doc = new jsPDF();
doc.lines([[50, 0], [0, 50], [-50, 0], [0, -50]], 20, 20, [1.0, 1.0], null, true); // horizontal line
doc.clip();
doc.rect(50, 50, 100, 100, 'F');
我成功剪裁了!
但是我不能使剪辑区域超过一个。
最佳答案
他们实际上是在最近解决了这个问题,但是显然不想破坏现有的API,因此他们添加了一个新方法,名称为clip_fixed
:
https://github.com/MrRio/jsPDF/commit/e32f0a2b222a940b0f228457371a118fc138ec28#diff-8162ee9f6251a2016741ffe67239c3c4
一旦使用null样式对象称为doc.clip_fixed()
绘制了剪切路径,随后绘制的所有形状都将被剪切到当前剪切路径。
如果此后如果您需要在不应用剪切的情况下绘制其他形状,您将意识到像doc.unclip()一样的东西。因此,以后绘制未剪切形状的最佳选择是在绘制剪切路径之前先将当前状态保存到“图形状态堆栈”中,然后在绘制完最后一个剪切形状后,从堆栈中恢复先前的状态。不幸的是,该堆栈的API尚未通过jsPDF公开,但是您可以通过doc.internal.write()
访问jsPDF的内部命令队列,以插入jsPDF尚未处理的任何PDF命令。
因此,要创建可逆剪辑,请执行以下操作:
doc.internal.write('q'); // saves the currrent state
// any draw command with a style value of null
doc.clip_fixed();
// any number of draw commands with strokes, fills, or null for compound paths
doc.internal.write('Q'); // restores the state to where there was no clipping
有关剪辑的进一步阅读:
http://www.verypdf.com/document/pdf-format-reference/pg_0234.htm
关于javascript - 如何在jsPDF中使用Clip方法?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44895953/