gregaryb Posted January 25, 2022 Share Posted January 25, 2022 (edited) This is the code I am using: function doDisplayHelper(Context, ComponentImage, nPosX, nPosY) { Context.drawImage(ComponentImage, nPosX, nPosY); Context.beginPath(); Context.rect(0, 0, nPosX, nPosY); Context.stroke(); } class CComponentBase { constructor() { this.m_strImageFile = ""; this.m_arrayPins = new CPinArray(); this.m_strDeviceName = ""; this.m_nPosX = 100; this.m_nPosY = 100; } getImageFileName() { return this.m_strImageFile; } setDeviceName(strDeviceName) { this.m_strDeviceName = strDeviceName; } getDeviceName() { return this.m_strDeviceName; } isSelected() { return false; } doSelect() { } doUnselect() { } doDisplay() { var Canvas = getElement("canvas"); var Context = null; var ComponentImage = null; var strImageID = this.getImageFileName(); var nPosX = this.m_nPosX, nPosY = this.m_nPosY; if (Canvas != null) { Context = Canvas.getContext("2d"); if (Context != null) { ComponentImage = new Image(); ComponentImage.src = strImageID; ComponentImage.onload = function() {doDisplayHelper(Context, ComponentImage, nPosX, nPosY);}; } } } } As you can see from the image below there is nothing wrong with the parameters to the doDisplayHelper function because I can draw a rectangle just fine. I just cannot get the image from the .png file to display. Also below is a break point in Firefox developer edition showing the var containing the .png file name. Which definitely exists and is correct. So why does my .png image refuse to display? Edited January 25, 2022 by gregaryb Link to comment Share on other sites More sharing options...
Ingolme Posted January 25, 2022 Share Posted January 25, 2022 Are there any errors in the browser console? Try putting the image path into an <img> tag on the page to see if it appears. If it doesn't, then the path isn't correct. Link to comment Share on other sites More sharing options...
gregaryb Posted January 26, 2022 Author Share Posted January 26, 2022 (edited) 10 hours ago, Ingolme said: Are there any errors in the browser console? Try putting the image path into an <img> tag on the page to see if it appears. If it doesn't, then the path isn't correct. What on earth has an img tag got to to with my canvas? I can't do that for every image that might be dragged and dropped on to my canvas though. Edited January 26, 2022 by gregaryb Link to comment Share on other sites More sharing options...
Ingolme Posted January 26, 2022 Share Posted January 26, 2022 I was just saying to use an <img> tag temporarily to test that the image URL actually works properly. If the image is not visible when using an <img> tag, it means that the URL is invalid. Link to comment Share on other sites More sharing options...
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