canvas.translate(x,y)单位

canvas.translate(x,y)这里的参数x,y都以像素为单位。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import QtQuick 2.4 import QtQuick.Controls 1.4 import QtQuick.Window 2.3 ApplicationWindow { visible: true width: 800 height: 600 title: "Drawing Board Example" Rectangle { width: 600 height: 400 MouseArea { id: mouseArea anchors.fill: parent hoverEnabled: true property bool isDragging: false property var startPoint: Qt.point(0,0) property var rect: { x: 0, y: 0, width: 100, height: 100, angle: 0 } // 绘制矩形 function drawRect() { canvas.getContext("2d").clearRect(0, 0, width, height); canvas.getContext("2d").save(); canvas.getContext("2d").translate(rect.x, rect.y); canvas.getContext("2d").rotate(rect.angle); canvas.getContext("2d").fillRect(-rect.width / 2, -rect.height / 2, rect.width, rect.height); canvas.getContext("2d").restore(); } // 鼠标事件处理 onPressed: { const x = mouse.x - width / 2; const y = mouse.y - height / 2; if (x > rect.x - rect.width / 2 && x < rect.x + rect.width / 2 && y > rect.y - rect.height / 2 && y < rect.y + rect.height / 2) { isDragging = true; startPoint = Qt.point(mouse.x, mouse.y); } } onPositionChanged:: { if (isDragging) { const dx = mouse.x - startPoint.x; const dy = mouse.y - startPoint.y; rect.x += dx; rect.y += dy; startPoint = Qt.point(mouse.x, mouse.y); drawRect(); } } onReleased: { isDragging = false; } onWheel: { const zoom = angleDelta.y > 0 ? 0.9 : 1.1; rect.width *= zoom; rect.height *= zoom; drawRect(); } // 画布 Canvas { id: canvas anchors.fill: parent contextType: "2d" // 设置画布初始位置和网格线样式 onPaint: { canvas.getContext("2d").translate(width / 2, height / 2); canvas.getContext("2d").lineWidth = 0.5; canvas.getContext("2d").strokeStyle = '#ddd'; // 绘制网格线 for (let i = -width / 2; i <= width / 2; i += 10) { canvas.getContext("2d").beginPath(); canvas.getContext("2d").moveTo(i, -height / 2); canvas.getContext("2d").lineTo(i, height / 2); canvas.getContext("2d").stroke(); } for (let i = -height / 2; i <= height / 2; i += 10) { canvas.getContext("2d").beginPath(); canvas.getContext("2d").moveTo(-width / 2, i); canvas.getContext("2d").lineTo(width / 2, i); canvas.getContext("2d").stroke(); } drawRect(); } } } } }这段代码中的property var rect: { x: 0, y: 0, width: 100, height: 100, angle: 0 }这一行发生报错,请基于qt的qml语言做出修改
06-09

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值