我可以使用function getVisiblePixelsBoundingBox(canvas) {
const context = canvas.getContext("2d");
const imageData = context.getImageData(0, 0, canvas.width, canvas.height).data;
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
for (let y = 0; y < canvas.height; y++) {
for (let x = 0; x < canvas.width; x++) {
const alpha = imageData[(y * canvas.width + x) * 4 + 3];
if (alpha > 0) {
minX = Math.min(minX, x);
minY = Math.min(minY, y);
maxX = Math.max(maxX, x);
maxY = Math.max(maxY, y);
}
}
}
return {
left: minX,
top: minY,
width: maxX - minX,
height: maxY - minY
};
}
// you will need to create a new canvas
var canvas = document.createElement("canvas"),
context = canvas.getContext("2d");
// and set the width and height the same as the canvas you want to draw the text
// here I'm just adding some values for the example:
canvas.width = 600;
canvas.height = 400;
// now I'm going to draw some text on it
context.font = "120px serif";
context.fillText("Lipsum", 100, 200);
let boundingBox = getVisiblePixelsBoundingBox(canvas);
// now this is not needed in your case, but want to see the canvas and
// I will draw a rectangle around the bounding box to see how it works:
document.body.appendChild(canvas);
context.strokeStyle = "red";
context.strokeRect(boundingBox.left, boundingBox.top, boundingBox.width, boundingBox.height);
和.pro
文件创建项目文件.cpp
,此GitHub demo文件包含.h
个文件,但没有CMakeLists.txt,MainWindow.cpp MainWindow.h,MainWindow.uimain.cpp
} file如何制作.pro
答案 0 :(得分:1)
为了执行此代码,第一步是生成.pro,它会打开一个终端并执行:
qmake -project
使用包含它的文件夹名称生成.pro文件,该文件将包含类似于以下内容:
<强> sizegripitem-master.pro 强>
######################################################################
# Automatically generated by qmake (3.1) Tue Oct 24 12:36:31 2017
######################################################################
TEMPLATE = app
TARGET = sizegripitem-master
INCLUDEPATH += .
# The following define makes your compiler warn you if you use any
# feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
# Input
HEADERS += SizeGripItem.h demo/MainWindow.h
FORMS += demo/MainWindow.ui
SOURCES += SizeGripItem.cpp demo/main.cpp demo/MainWindow.cpp
之后我们必须指出模块,因为我看到类只需要模块core,gui和widget,我们添加以下内容:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
您应该做的另一件事是更新一行代码以与Qt5兼容,在main.cpp文件中更改:
#include <QtGui/QApplication>
为:
#include <QApplication>