Jump to content

Recommended Posts

I want to render a rectangle to the cursor, but the rectangle too far from the cursor.. :S How to fix this in my code?

code:

local sx_, sy_ = 1920, 1080
local sx__, sy__ = guiGetScreenSize()
local xm, ym = sx__/sx_, sy__/sy_

function render()
local screenx, screeny = getCursorPosition()

dxDrawRectangle((sx_-687) * xm * screenx, (sy_/2+70) * ym * screeny, 170 * xm, 25 * ym,tocolor(255,181,64,80))
end
addEventHandler("onClientRender",root,render)

the problem:

82fQ2n9.png

Link to post

You want to draw rectangle next to cursor, am i right?

getCursorPosition returns values from 0 to 1 so you should multiply values to get absolute coordinates like this:

local scx, scy = guiGetScreenSize()
local cx, cy = getCursorPosition()

addEventHandler("onClientRender", root, function()
    dxDrawRectangle(cx*scx, cy*scy, 300, 100)
end)

 

  • Thanks 1
Link to post
40 minutes ago, JeViCo said:

You want to draw rectangle next to cursor, am i right?

getCursorPosition returns values from 0 to 1 so you should multiply values to get absolute coordinates like this:


local scx, scy = guiGetScreenSize()
local cx, cy = getCursorPosition()

addEventHandler("onClientRender", root, function()
    dxDrawRectangle(cx*scx, cy*scy, 300, 100)
end)

 

Thank you! And how can i increase the rectangle height with the text, like the width with dxGetTextWidth?

Link to post

Example code:

local scx, scy = guiGetScreenSize()
local text = "hello world"
showCursor(true)

addEventHandler("onClientRender", root, function()
    if isCursorShowing() then -- can be removed
    	local cx, cy = getCursorPosition()
    	local boxW = dxGetTextWidth(text) + 2*20 -- add left and right paddings
    	local boxH = dxGetFontHeight() + 2*10 -- add top and bottom paddings

    	dxDrawRectangle(cx*scx, cy*scy, boxW, boxH)
    	dxDrawText(text, cx*scx, cy*scy, boxW+cx*scx, boxH+cy*scy, tocolor(0, 0, 0, 255), 1, "default", "center", "center")
    end
end)

Result:
screenshot_1.jpg

Also don't forget about scale, font and color-coded properties which affect on dxGetTextWidth, dxGetFontHeight and drawing functions

  • Thanks 1
Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...