Turbe$Z Posted November 16, 2019 Share Posted November 16, 2019 I want to render a rectangle to the cursor, but the rectangle too far from the cursor.. 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: Link to comment
JeViCo Posted November 16, 2019 Share Posted November 16, 2019 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) 1 Link to comment
Turbe$Z Posted November 16, 2019 Author Share Posted November 16, 2019 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 comment
Overkillz Posted November 16, 2019 Share Posted November 16, 2019 Dont forget to use isCursorShowing() to prevent errors while rendering. Link to comment
JeViCo Posted November 17, 2019 Share Posted November 17, 2019 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: Also don't forget about scale, font and color-coded properties which affect on dxGetTextWidth, dxGetFontHeight and drawing functions 1 Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now