javascript - 如何使用 PDF.js 和 jQuery 在 PDF 上创建可拖动元素
问题描述
我正在创建一项服务,使用 PDF.js 和 jQuery 用图像标记 pdf。我设法使用 PDF.js 创建了一个可拖动对象,但是该对象在被拖动时会留下过去对象的痕迹。
我曾经context.clearRect(0, 0, canvas.width, canvas.height);
清除过去的对象,但它也清除了画布中的基础 PDF。
如何在不影响底层 PDF 的情况下拖动此对象?
这是我到目前为止所做的。
我正在使用以下代码将 PDF 加载到画布上。
function loadPdfPreview(base64pdf){
pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.0.943/pdf.worker.min.js';
var loadingTask = pdfjsLib.getDocument({data: base64pdf});
loadingTask.promise.then(function (pdf) {
// Fetch the first page
pdf.getPage(1).then(function (page) {
var scale = 1.0;
var viewport = page.getViewport(scale);
// Prepare canvas using PDF page dimensions
canvas = document.getElementById('pdf-canvas');
context = canvas.getContext('2d');
canvas.height = viewport.height;
canvas.width = viewport.width;
canvasOffset = $("#pdf-canvas").offset();
offsetX = canvasOffset.left;
offsetY = canvasOffset.top;
// Render PDF page into canvas context
var renderContext = {
canvasContext: context,
viewport: viewport
};
page.render(renderContext).then(function () {
// creating the dummy object on success
drawObjectFromBlueprint(blueprint);
}, function (e) {
console.log(e);
});
});
});
}
在将 pdf 加载到画布后,我使用以下函数在画布顶部绘制对象。
function drawObjectFromBlueprint(blueprint) {
// drawing a draggable element inside the canvas
context.strokeStyle = "lightgray";
// Clearing the previous dummy objects
context.clearRect(0, 0, canvas.width, canvas.height);
// drawing the dummy object
context.beginPath();
context.moveTo(blueprint.x, blueprint.y);
context.lineTo(blueprint.right, blueprint.y);
context.lineTo(blueprint.right, blueprint.bottom);
context.lineTo(blueprint.x, blueprint.bottom);
context.closePath();
context.fillStyle = blueprint.fill;
context.fill();
context.stroke();
}
我使用以下代码处理鼠标移动事件。
function handleMouseMove(e) {
var mouseX = parseInt(e.clientX - offsetX1);
var mouseY = parseInt(e.clientY - offsetY1);
// always update the global blueprint
blueprint.x += (mouseX - lastX);
blueprint.y += (mouseY - lastY);
blueprint.right = blueprint.x + blueprint.width;
blueprint.bottom = blueprint.y + blueprint.height;
lastX = mouseX;
lastY = mouseY;
drawObjectFromBlueprint(blueprint);
}
我使用以下代码监听鼠标移动事件。
$("#drawable-canvas").mousemove(function (e) {
handleMouseMove(e);
});
我想在不影响底层 PDF 的情况下重新绘制对象。试图通过清除整个画布来加载 PDF 并重新绘制对象,但它没有按预期工作。
解决方案
在该page.render
方法的回调中,会在画布上绘制pdf页面。您必须单独保存绘制的图像,以便原始图像不会因拖动而消失。
// maybe globalScope...?
const tempCanvas = document.createElement('canvas');
const tempCtx = tempCanvas.getContext('2d');
// your code
page.render(renderContext).then(function () {
// Save the original page image.
tempCanvas.width = canvas.width;
tempCanvas.height = canvas.height;
tempCtx.drawImage(canvas, 0, 0);
// creating the dummy object on success
drawObjectFromBlueprint(blueprint);
}, ...
接下来,请修改它以使用 中的原始页面图像进行绘制drawObjectFromBlueprint
。
function drawObjectFromBlueprint(blueprint) {
// draw original page image
context.drawImage(tempCanvas, 0, 0);
// drawing a draggable element inside the canvas
context.strokeStyle = "lightgray";
...
}
推荐阅读
- python - 获取熊猫数据框中为真的列名
- tensorflow - TensorFlow lite 转换:错误:op 操作数必须是 8 位无符号整数的张量,但得到 tensor<1x?x?x3x!tf.quint8>
- python - Pymc3 类型错误 __init__() 得到了一个意外的关键字参数 'coords'
- python-3.x - 解决汽车多车道行驶仿真问题
- tkinter - Python tkinter 画布几何
- c# - C#如何在程序中定义方法之前调用它?
- python - 访问 pandas to_csv 中的 zip 压缩选项
- node.js - 如何在打字稿中实现类型安全、基于幻象类型的构建器?
- ios - iOS 模拟器版本没有出现 - Xcode 12.2
- ruby-on-rails - Ruby on Rails - 设计 Omniauth - Coinbase 策略 - 传递帐户参数