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

  • Home
  • SEARCH
  • 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 7655239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:27:46+00:00 2026-05-31T12:27:46+00:00

I’m trying to construct a base class Shape using Crockford’s inheritance pattern. Using this

  • 0

I’m trying to construct a base class Shape using Crockford’s inheritance pattern. Using this base Shape, I’m trying to draw a circle, a rectangle and a triangle. I’m kinda stuck. I didn’t know how to call/modify the base method

function points(x,y) {
     x = this.x;
     y = this.y;
}

function Shape() {
    return {
            this.points: [ ],

    init : function(){
    if(typeof this.context === ‘undefined’){
            var canvas = document.getElementById(‘canvas’);
                var context = canvas.getContext(‘2d’);
            }
     },
     draw: function(){ 
             var context = this.context;
             context.beginPath();
             context.moveTo(this.points[0].x, this.points[0].y);
             for(var i=1; i< this.parameter.length; i++){
                context.lineTo(this.parameter[i].x, this.parameter[i].y);
             }
             context.closePath();
             context.stroke();
     }
};
}

function Circle(x, y, r){
var points = Shape();
    point.x = x;
    points.y = y;
    points.r = r; 
    var baseMethod = that.draw;
       that.draw = function(){
       /*how to modify the base method to draw circle*/
    };

}
function Rectangle(a, b, c, d){
var points = Shape();
    point.a = a;
    points.b = b;
    points.c = c;
    points.d = d 
    var baseMethod = that.draw;
       that.draw = function(){
       /*how to call base method to draw rectangle*/
    };

}
  • 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-05-31T12:27:47+00:00Added an answer on May 31, 2026 at 12:27 pm

    You’ve got quite a few problems going on with your code. Firstly you need to make sure you’ve got your basic drawing code working before moving on to more complicated shapes such as circles and rectangles. Start with drawing lines. I’ve tidied up your code and got it working with drawing straight lines:

    //returns basic point object which has
    //two properties x & y
    function point(x, y) {
        return {
            x: x,
            y: y
        }
    }
    
    
    //function that returns a shape object with all the 
    //mechanisms for drawing lines between points
    function Shape(canvasID) {
        return {
            points: [], //not 'this.points' (which would most likely be window.points)
            addPoint: function(x, y) {//adding a point to a shape is an operation of shape
                this.points.push(point(x, y))
            },
            init: function() {
                if (typeof this.context === 'undefined') {
                    var canvas = document.getElementById(canvasID);
                    var ctx = canvas.getContext('2d');
                    this.context = ctx; //add the context reference to the current shape object
                }
            },
            draw: function() {
                this.init();
                var context = this.context;
                context.beginPath();
                var that = this; //create a local reference to the current 'this' object.
                //insures us against any possible 'this' scope problems
                context.moveTo(that.points[0].x, that.points[0].y);
                for (var i = 1; i < that.points.length; i++) {
                    context.lineTo(that.points[i].x, this.points[i].y);
                }
                context.closePath();
                context.stroke();
            }
        };
    }
    
    //Simple Line object - good for testing your
    //basic drawing functionality
    function Line(canvasID, x, y, x2, y2) {
        var shape = Shape(canvasID);
        shape.addPoint(x, y);
        shape.addPoint(x2, y2);
        shape.draw();
    
    }
    
    //Execute your drawing functionality after the 
    //window has loaded to make sure all your objects exist before 
    //trying to use them
    window.onload = function() {
        Line('canvas', 100, 100, 200, 200);
    }
    

    I’m not necessarily sold on whether this is the best way to approach what you are doing – but DC’s basic approach is to create objects without having to use the “new” keyword. So he returns an object from a function call using the JavaScript object notation.

    Now that you can draw a line, the next step is to draw a series of connected lines one after the other (a path). After that, create your rectangle. You need some code to tell your code where to start drawing the rectangle (the start x/y coordinate) and then you can have parameters denoting the height and width of the rectangle which will be used to calculate the coordinates of the rectangle’s corners and passed to the shape object to be drawn in the same way the series of connected lines were drawn. One caveat, though, is to check if there is some sort of ‘createRectangle’ function on the context object (and same for circle). I don’t actually know myself as I’ve not done this sort of work in HTML5/canvas – although I have in other environments.

    Edit

    Forgot to mention that you will need to make sure the doctype declaration of your html is html5. A lot of IDE’s will automatically declare your html as html4. Html5 just needs: <!DOCTYPE html>

    Also, make sure you declare a canvas element in the html body, something like this:

    <canvas id="canvas" width="300" height="150">   
       </canvas>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the

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.