Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8009169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:21:58+00:00 2026-06-04T18:21:58+00:00

I am using the Canvas 2d context to write text to the screen.. To

  • 0

I am using the Canvas 2d context to write text to the screen..

To accomplish this I have it run through an array of text objects that I created, right now I have the text objects with 3 properties:

text.text
text.x
text.y

text.text holds the string to write, text.x holds the value for the x position, and text.y holds the value for the y position

Is there anyway I could skip the text.text property?

so for example, right now it looks something like this:

var textStrings = [];

textStrings[0] = {};
textStrings[0].text = "hello";
textStrings[0].x = 0;
textStrings[0].y = 10;

textStrings[1] = {};
textStrings[1].text = "world";
textStrings[1].x = 10;
textStrings[1].y = 10;

But is there any way that I could do something like this instead:

textStrings = [];
textStrings[0] = {};
textStrings[0] = "hello";
textStrings[0].x = "0";
textStrings[0].y = 10;

textStrings[1] = {};
textStrings[1] = "world";
textStrings[1].x = 10;
textStrings[1].y = 10;

basically a default property of an object or something…

right now as soon as I do something like

textStrings[0] = "hello";

it changes textStrings to a string instead of an object, and then I can no longer add properties to it, since its a primitive data type.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:21:59+00:00Added an answer on June 4, 2026 at 6:21 pm

    You can use String objects instead of primitive values:

    var a = new String("hello");
    a.x = "0";
    a.y = 10;
    var b = new String("world");
    b.x = "10";
    b.y = 10;
    
    var textStrings = [a, b];
    

    You might also use special objects. The toString method is automatically used when the object is to be converted to a string:

    function Text(t, x, y) {
        this.text = t; this.x = x; this.y = y;
    }
    Text.prototype.toString = function() { return this.text; }
    alert(new Text("hello", 0, 0)); // implicit string conversion
    console.log(""+new Text("world", 10, 10)); // explicit string conversion
    

    I guess you would have such a constructor function anyway, to simplify the syntax for the textStrings array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a HTML5-canvas and write text with context.fillText(...); using a @font-face-font. Displaying the
I'm drawing simple text in HTML5 canvas using this: context.fillStyle = #FF0000 context.font =
I am using canvas,and I have this code - var myCanvas = document.getElementById(myCanvas), ctx
When writing pixels to an HTML Canvas context using putImageData I find that the
I have created a triangle using canvas but i was wondering if there was
I have a JS library using canvas that needs to get its colors from
I am using Canvas.TextRect in Delphi 7 to output something on the screen. I
I am using canvas.translate to change the coodinates of my canvas so that the
I have a static image (a png) that I'm drawing on a canvas element
I just create a app for writing the text on Screen using TouchEvent method.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.